-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradixsort.cpp
More file actions
44 lines (40 loc) · 844 Bytes
/
radixsort.cpp
File metadata and controls
44 lines (40 loc) · 844 Bytes
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
#include <iostream>
#include <vector>
using namespace std;
// User function Template for C++
void countSort(int arr[],int pos,int n){
vector<int> freq(10,0);
for(int i=0;i<n;i++){
freq[(arr[i]/pos)%10]++;
}
for(int i=1;i<10;i++){
freq[i]+=freq[i-1];
}
vector<int> ans(n);
for(int i=n-1;i>=0;i--){
ans[--freq[(arr[i]/pos)%10]]=arr[i];
}
for(int i=0;i<n;i++){
arr[i] =ans[i];
}
}
void radixSort(int arr[], int n) {
// code here
int max=-1;
for(int i=0;i<n;i++){
if(max<arr[i]) max=arr[i];
}
for(int i=1;max/i>0;i=i*10){
countSort(arr,i,n);
}
}
int main()
{
int arr[] ={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
radixSort(arr,10);
for (int i = 0; i <10; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}