forked from govind2006/codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix.c
54 lines (52 loc) · 1000 Bytes
/
radix.c
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
#include <stdio.h>
int a[100], b[100];
int length(int m) {
int p = 0;
while (m > 0) {
m = m / 10;
p++;
}
return p;
}
int max(int g[], int n) {
int m = 0, i;
for (i = 1; i <= n; i++)
if (g[i] > m)
m = g[i];
return m;
}
void countsort(int n, int div) {
int c[10], i;
for (i = 0; i <= 9; i++)
c[i] = 0;
for (i = 1; i <= n; i++)
c[(a[i] / div) % 10]++;
for (i = 1; i <= 9; i++)
c[i] = c[i - 1] + c[i];
for (i = 0; i < n; i++) {
b[c[(a[n - i] / div) % 10]] = a[n - i];
c[(a[n - i] / div) % 10]--;
}
for (i = 1; i <= n; i++)
a[i] = b[i];
}
void radix(int n) {
int m = max(a, n), div = 1, i;
int l = length(m);
for (i = 0; i < l; i++) {
countsort(n, div);
div = div * 10;
}
}
int main() {
int i, n, x;
printf("enter size=");
scanf("%d", &n);
printf("enter value\n");
for (i = 1; i <= n; i++)
scanf("%d", &a[i]);
radix(n);
printf("sorted array=");
for (i = 1; i <= n; i++)
printf("%d ", a[i]);
}