-
Notifications
You must be signed in to change notification settings - Fork 12
/
radix sort
151 lines (69 loc) · 1.22 KB
/
radix sort
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include<stdio.h>
#include<conio.h>
int radix_sort(int array[], int n);
int main()
{
int array[100],n,i;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
printf("\nEnter the elements to be sorted: \n");
for(i = 0 ; i < n ; i++ )
{
scanf("%d",&array[i]);
}
printf("\nBefore Radix Sort:");
for(i = 0; i < n; i++)
{
printf("%d\t", array[i]);
}
printf("\n");
radix_sort(array,n);
printf("\nArray After Radix Sort: "); //Array After Radix Sort
for(i = 0; i < n; i++)
{
printf("%d\t", array[i]);
}
printf("\n");
getch();
}
radix_sort(int arr[], int n)
{
int bucket[10][5],buck[10],b[10];
int i,j,k,l,num,div,large,passes;
div=1;
num=0;
large=arr[0];
for(i=0 ; i < n ; i++)
{
if(arr[i] > large)
{
large = arr[i];
}
while(large > 0)
{
num++;
large = large/10;
}
for(passes=0 ; passes < num ; passes++)
{
for(k=0 ; k < 10 ; k++)
{
buck[k] = 0;
}
for(i=0 ; i < n ;i++)
{
l = ((arr[i]/div)%10);
bucket[l][buck[l]++] = arr[i];
}
i=0;
for(k=0 ; k< 10 ; k++)
{
for(j=0 ; j < buck[k] ; j++)
{
arr[i++] = bucket[k][j];
}
}
div*=10;
}
}
}