Any optimizations?
Code:// v is the vector to sort // min is the smallest item in the array // max is the largest. // counting sort functions in O(range * n) time void counting_sort(vector<int>& v, int min, int max) { int range = max - min + 1; int* counts = new int[range]; int offset = -min; vector<int>::size_type size = v.size(); memset(counts, 0, range * sizeof(int)); for(int i = 0; i < size; ++i) ++counts[v[i] + offset]; int index = 0; for(int i = 0; i < range; ++i) while(counts[i]--) v[index++] = i - offset; delete [] counts; return; }





Reply With Quote