Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int getMax(pair<string, int> * arr, int n){
  7.     int max = arr[0].second;
  8.     for (int i = 1; i < n; i++)
  9.         if (arr[i].second > max)
  10.             max = arr[i].second;
  11.     return max;
  12. }
  13.  
  14. void radixSort( pair<string, int> * arr, int n){
  15.    int max = getMax(arr, n);
  16.    for (int pos = 1; max / pos > 0; pos = pos * 10){
  17.         int i;
  18.         int count_array[10] = {0};
  19.         pair<string, int> * output_array = new pair<string, int>[n];
  20.  
  21.         for (i = 0; i < n; i++)
  22.             count_array[ arr[i].second / pos % 10 ]++;
  23.  
  24.         for (i = 1; i < 10; i++)
  25.             count_array[i] = count_array[i] + count_array[i - 1];
  26.  
  27.         for (i = n - 1; i >= 0; i--){
  28.             output_array[count_array[ arr[i].second / pos % 10 ] - 1] = arr[i];
  29.             count_array[ arr[i].second / pos % 10 ]--;
  30.         }
  31.  
  32.         for (i = 0; i < n; i++)
  33.             arr[i] = output_array[i];
  34.  
  35.         delete [] output_array;
  36.     }
  37. }
  38.  
  39. int main(){
  40.     std::ios::sync_with_stdio(false);
  41.     int n;
  42.     cin >> n;
  43.     pair<string, int> * arr = new pair<string, int>[n];
  44.     string name;
  45.     int cals;
  46.     for(int i = 0; i < n; i++){
  47.         cin >> name >> cals;
  48.         arr[i].first = name;
  49.         arr[i].second =  cals;
  50.     }
  51.     radixSort(arr, n);
  52.     for(int i = 0; i < n; i++){
  53.         cout << arr[i].first << " ";
  54.     }
  55.     delete [] arr;
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement