I am implementing quickSort using my own namespace alg
.
namespace alg
{
static unsigned int comparisons(0);
void quickSort (std::vector<int>& a);
int partition (std::vector<int>& b, int leftIndex);
}
I need to count comparisons of elements in the array, for that reason I use static variable comparisons
that GETS incremented in functions but in main.cpp
it still stays 0.
What is wrong? Why in main.cpp I guess for every .cpp
file there is a serapate static variable comparisons
, but I am not sure. Anyway, how can I fix this? Do I actually need to write a class where I can keep a static variable? Or that still won't help?
And Please suggest where can I find detailed information on static variables and namespaces?
QuickSort.cpp
void alg::quickSort(std::vector<int>& a)
{
....
comparisons+=1;
}
int alg::partition(std::vector<int>& a, int leftIndex)
{
comparisons+=a.size()-1;
....
}
main.cpp
alg::quickSort(myvec);
...
std::cout << alg::comparisons << std::endl;