fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void CountingSort(int from[], int to[], int len, int maxval) {
  5. int *count = new int[maxval + 1];
  6.  
  7. for (int i = 0; i < (maxval + 1); i++)
  8. count[i] = 0;
  9. for (int j = 0; j < len; j++)
  10. count[from[j]]++;
  11.  
  12. int curridx = 0;
  13. for (int val = 0; val <= maxval; val++) {
  14. for (int counter = 0; counter < count[val]; counter++)
  15. to[curridx++] = val;
  16. }
  17. delete[] count;
  18. }
  19.  
  20. int main()
  21. {
  22. int v[10] = {9, 0, 3, 4, 1, 2, 7, 8, 5, 6};
  23. int u[10];
  24.  
  25. CountingSort(v, u, 10, 9);
  26.  
  27. for (int i = 0; i < 10; i++)
  28. std::cout << u[i] << ' ';
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 6 7 8 9