Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- int getMax(pair<string, int> * arr, int n){
- int max = arr[0].second;
- for (int i = 1; i < n; i++)
- if (arr[i].second > max)
- max = arr[i].second;
- return max;
- }
- void radixSort( pair<string, int> * arr, int n){
- int max = getMax(arr, n);
- for (int pos = 1; max / pos > 0; pos = pos * 10){
- int i;
- int count_array[10] = {0};
- pair<string, int> * output_array = new pair<string, int>[n];
- for (i = 0; i < n; i++)
- count_array[ arr[i].second / pos % 10 ]++;
- for (i = 1; i < 10; i++)
- count_array[i] = count_array[i] + count_array[i - 1];
- for (i = n - 1; i >= 0; i--){
- output_array[count_array[ arr[i].second / pos % 10 ] - 1] = arr[i];
- count_array[ arr[i].second / pos % 10 ]--;
- }
- for (i = 0; i < n; i++)
- arr[i] = output_array[i];
- delete [] output_array;
- }
- }
- int main(){
- std::ios::sync_with_stdio(false);
- int n;
- cin >> n;
- pair<string, int> * arr = new pair<string, int>[n];
- string name;
- int cals;
- for(int i = 0; i < n; i++){
- cin >> name >> cals;
- arr[i].first = name;
- arr[i].second = cals;
- }
- radixSort(arr, n);
- for(int i = 0; i < n; i++){
- cout << arr[i].first << " ";
- }
- delete [] arr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement