-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRadix_sort.cpp
68 lines (68 loc) · 1.22 KB
/
Radix_sort.cpp
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
#include<bits/stdc++.h>
using namespace std;
vector<int>v[10];
int n;
map<int,int>mp;
int ndigit(int d,int temp)
{
int p;
while(d)
{
p=temp%10;
temp/=10;
d--;
}
return p;
}
int radix(int arr[],int p)
{
int i,j,cnt=0,temp,dig,k,qw,l,mn,loc;
for(i=1; i<=p; i++)
{
qw=0;
loc=0;
for(j=0; j<n; j++)
{
dig=ndigit(i,arr[j]);
v[dig].push_back(arr[j]);
}
for(j=0; j<=9; j++)
{
l=v[j].size();
mn=0;
//cout<<l<<" ";
for(k=loc; k<n; k++)
{
if(l)
{
arr[k]=v[j][mn++];
l--;
loc++;
}
}
}
for(j=0;j<10;j++)
v[j].clear();
}
}
int main()
{
int arr[100],maxx,p=0;
cin>>n;
cin>>arr[0];
maxx=arr[0];
for(int i=1; i<n; i++)
{
cin>>arr[i];
maxx=max(maxx,arr[i]);
}
while(maxx>0)
{
maxx/=10;
p++;
}
radix(arr,p);
for(int i=0; i<n; i++)
cout<<arr[i]<<" ";
cout<<endl;
}