Advertisement
Tevronis

radix sort

Oct 27th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <deque>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <map>
  9. #include <set>
  10.  
  11. using namespace std;
  12. typedef pair<int, int> pii;
  13. #define mp make_pair
  14. const int N = 100;
  15. vector<int> arr;
  16.  
  17. int digit(int x, int ind) {
  18.     int cnt = 0;
  19.  
  20.     while (cnt != ind) {
  21.         x /= 10;
  22.         cnt++;
  23.     }
  24.     return x % 10;
  25. }
  26.  
  27. void radix_sort(int l, int r) {
  28.     //k = max len;
  29.     int k = 32;
  30.     vector<int > b(arr.size());
  31.     for (int i = 0; i < k; i++) {
  32.         vector<int> c(10);
  33.         for (int j = 0; j < N; j++) {
  34.             c[digit(arr[j], i)]++;
  35.         }
  36.         for (int j = 1; j < 10; j++) {
  37.             c[j] += c[j - 1];
  38.         }
  39.         for (int j = N - 1; j >= 0; j--) {
  40.             b[--c[digit(arr[j], i)]] = arr[j];
  41.         }
  42.         arr = b;
  43.     }
  44. }
  45.  
  46.  
  47. int main() {
  48. #ifdef _DEBUG
  49.     freopen("input.txt", "r", stdin);
  50.     freopen("output.txt", "w", stdout);
  51. #endif
  52.     arr.resize(N);
  53.  
  54.     for (int i = 0; i < N; i++) {
  55.         int x = rand() ^ rand();
  56.         arr[i] = x;
  57.     }
  58.  
  59.     radix_sort(0, N-1);
  60.     for (int i = 0; i < N; i++) {
  61.         cout << arr[i] << '\n';
  62.     }
  63.     return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement