File tree Expand file tree Collapse file tree 1 file changed +72
-0
lines changed Expand file tree Collapse file tree 1 file changed +72
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+
3
+ using namespace std ;
4
+
5
+ void radixSort (int *arr, int &no_of_elmnts){
6
+ int min = INT_MAX;
7
+ int max = INT_MIN;
8
+
9
+ for (int i=0 ;i<no_of_elmnts;i++){
10
+ if (arr[i]>max){
11
+ max = arr[i];
12
+ continue ;
13
+ }
14
+ if (arr[i]<min){
15
+ min=arr[i];
16
+ }
17
+ }
18
+
19
+ if (min<0 ){
20
+ for (int i=0 ;i<no_of_elmnts;i++){
21
+ arr[i]+=abs (min);
22
+ }
23
+ max+=abs (min);
24
+ }
25
+
26
+ int rounds=0 ;
27
+ while (max){
28
+ rounds++;
29
+ max/=10 ;
30
+ }
31
+
32
+ vector< list<int > > buckets;
33
+ int place_value=1 ;
34
+ for (int i=0 ;i<rounds;i++){
35
+ buckets.clear ();
36
+ buckets.resize (10 );
37
+
38
+ for (int j=0 ;j<no_of_elmnts;j++){
39
+ buckets[ (arr[j]/place_value) % 10 ].push_back (arr[j]);
40
+ }
41
+
42
+ int arr_index=0 ;
43
+ for (int j=0 ;j<10 ;j++){
44
+ list<int >::iterator it;
45
+ for (it=buckets[j].begin (); it!=buckets[j].end ();it++){
46
+ arr[arr_index++]=*it;
47
+ }
48
+ }
49
+ place_value*=10 ;
50
+ }
51
+
52
+ if (min<0 ){
53
+ for (int i=0 ;i<no_of_elmnts;i++){
54
+ arr[i]+=min;
55
+ }
56
+ }
57
+ }
58
+ int main (){
59
+ // freopen("input.txt", "rd", stdin);
60
+
61
+ int no_of_elmnts;
62
+ cin>>no_of_elmnts;
63
+
64
+ int *arr = new int [no_of_elmnts];
65
+
66
+ for (int i=0 ;i<no_of_elmnts;i++){
67
+ cin>>arr[i];
68
+ }
69
+ radixSort (arr, no_of_elmnts);
70
+
71
+ return 0 ;
72
+ }
You can’t perform that action at this time.
0 commit comments