fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void DisCountSort(vector<int> &arr, int n)
  8. {
  9. auto _max = max_element(arr.begin(), arr.end());
  10. int max_val = *_max;
  11. arr.clear(); // begin to sort arr
  12. vector<int> c(max_val + 1, 0); // number of times that index value appears
  13. for (int i = 0; i < n; ++i) c[arr[i]] += 1;
  14. auto it = arr.begin();
  15. for (int i = 0; i < max_val + 1; ++i) {
  16. if (c[i] > 0) {
  17. arr.insert(it, c[i], i);
  18. it = arr.end();
  19. }
  20.  
  21. }
  22. }
  23.  
  24. int main()
  25. {
  26. vector<int> arr{30, 90, 60, 70, 10, 60, 30, 70, 100, 50, 70};
  27. DisCountSort(arr, arr.size());
  28. for (auto &it : arr) cout << it << " ";
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
10 30 30 50 60 60 70 70 70 90 100