ulvis.paste.net

Paste Search Dynamic
Recent pastes
sort
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. void DisCountSort(vector<int> &arr)
  8. {
  9.     auto max_val = *max_element(arr.begin(), arr.end());
  10.     vector<int> c(max_val + 1, 0); // number of times that index value appears
  11.     for (auto x: arr) c[x]++;
  12.     arr.clear(); // begin to sort arr
  13.     for (int i = 0; i <= max_val; ++i) {
  14.         if (c[i] > 0) {
  15.             arr.insert(arr.end(), c[i], i);
  16.         }
  17.     }
  18. }
  19.  
  20. int main()
  21. {
  22.     vector<int> arr{30, 90, 60, 70, 10, 60, 30, 70, 100, 50, 70};
  23.     DisCountSort(arr);
  24.     for (auto &it : arr) cout << it << " ";
  25.     return 0;
  26. }
Parsed in 0.005 seconds