标签:
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 void COUNTING_SORT(int *const A, const int &length,int &k){ 6 7 vector<int> B(length, 0); 8 vector<int> C(k, 0); 9 10 for (auto j = 0; j < length; ++j){ 11 C[A[j]] = C[A[j]] + 1; 12 } 13 for (auto i = 1; i < k; ++i){ 14 C[i] = C[i] + C[i - 1]; 15 } 16 17 for (auto j = length-1; j >= 0; --j){ 18 B[C[A[j]]-1] = A[j]; 19 C[A[j]] = C[A[j]] - 1; 20 } 21 22 for (auto it = B.begin(); it != B.end(); ++it){ 23 cout << *it << " "; 24 } 25 cout << endl; 26 } 27 int main(void) 28 { 29 int a[] = { 2,5,3,0,2,3,0,3 }; 30 int i = 6; 31 COUNTING_SORT(a, (end(a) - begin(a)), i); 32 system("pause"); 33 return 0; 34 }
标签:
原文地址:http://www.cnblogs.com/lhyz/p/4306126.html