fork(1) 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 *topidx = new int[maxval + 1];
  13. topidx[0] = count[0];
  14. for (int i = 1; i < (maxval + 1); i++)
  15. topidx[i] = count[i] + topidx[i - 1];
  16. delete[] count;
  17.  
  18. for (int j = len - 1; j >= 0; j--) {
  19. int value = from[j];
  20. topidx[value]--;
  21. to[topidx[value]] = value;
  22. }
  23. delete[] topidx;
  24. }
  25.  
  26. int main()
  27. {
  28. int v[10] = {9, 0, 3, 4, 1, 2, 7, 8, 5, 6};
  29. int u[10];
  30.  
  31. CountingSort(v, u, 10, 9);
  32.  
  33. for (int i = 0; i < 10; i++)
  34. std::cout << u[i] << ' ';
  35.  
  36. return 0;
  37. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0 1 2 3 4 5 6 7 8 9