-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathRadixSort.cpp
More file actions
70 lines (57 loc) · 1.88 KB
/
RadixSort.cpp
File metadata and controls
70 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<bits/stdc++.h>
using namespace std;
// Time Complexcity = O(d*n)
// where d is max no. of digits in a number and n is size of ar
// Ref -> https://www.youtube.com/watch?v=a5e7RgCdel0
// Question -> Max Gap Leetcode
// We use pegion hole principle + Radix Sort to get ans in O(n) time.
// elements inside a bucket = (max_val-min_val)/n-1;
// Note bucket index (freq_index here) can be caluclated as (arr[i]-min)/interval
void countSort(vector<int>& arr, int exp) {
vector<int> freq(10,0);
vector<int> ans(arr.size());
for(int i=0;i<arr.size();i++)
freq[(arr[i]/exp) %10]++; // arr[i]/100 %10 gives ele at 1000th place
vector<int> prefix_index(10,0);
for(int i=0;i<10;i++){
prefix_index[i] = freq[i];
if(i != 0)
prefix_index[i] += prefix_index[i-1];
}
// To convert this prefix arr to index arr
// Each value in prefix[i] represnts max index of i
for(int i=0;i<freq.size();i++)
prefix_index[i]--;
// We traverse in reverse as we want to keep Radix sort stable
for(int i=arr.size()-1;i>=0;i--){
int index = (arr[i]/exp)%10;
ans[prefix_index[index]] = arr[i];
prefix_index[index]--;
}
arr = ans;
}
// Works for +ve elements
void radixSort(vector<int> &arr){
int i,max_val = INT_MIN;
for(i=0;i<arr.size();i++)
max_val = max(max_val,arr[i]);
int exp = 1;
// Loop till the msb of max_val
while(exp <= max_val) {
// apply count sort on (exp)'s place
countSort(arr,exp);
exp *= 10;
}
}
void print(vector<int> arr){
cout << "\nSorted Array is : ";
for(int i=0;i<arr.size();i++)
cout << arr[i] <<" ";
cout << endl;
}
int main(){
int n = 10;
vector<int> arr = {4533,6534,5881,2896,104,7435,8236,7865,909,6450};
radixSort(arr);
print(arr);
}