forked from VAR-solutions/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.c
More file actions
79 lines (70 loc) · 1.54 KB
/
radix_sort.c
File metadata and controls
79 lines (70 loc) · 1.54 KB
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
/*Radix sort
A sorting technique that sorts the elements by first grouping the individual digits of the same place value.
Then, sort the elements according to their increasing/decreasing order.*/
#include <stdio.h>
int max(int num[], int n);
void radix_sort(int num[], int n);
int main()
{
int i, n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int num[n];
printf("Enter the numbers: ");
for (i = 0; i < n; i++)
{
scanf("%d", &num[i]);
}
radix_sort(num, n);
printf("\n The Sorted list is : ");
for (i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n");
return 0;
}
int max(int num[], int n)
{
int max = num[0];
for (int i = 1; i < n; i++)
if (num[i] > max)
max = num[i];
return max;
}
void radix_sort(int num[], int n)
{
int box[10][10], box_cnt[10];
int i, j, k, r, NOP = 0, divisor = 1, lar, pass;
lar = max(num, n);
while (lar > 0)
{
NOP++;
lar /= 10;
}
for (pass = 0; pass < NOP; pass++)
{
for (i = 0; i < 10; i++)
{
box_cnt[i] = 0;
}
for (i = 0; i < n; i++)
{
r = (num[i] / divisor) % 10;
box[r][box_cnt[r]] = num[i];
box_cnt[r] += 1;
}
i = 0;
for (k = 0; k < 10; k++)
{
for (j = 0; j < box_cnt[k]; j++)
{
num[i] = box[k][j];
i++;
}
}
divisor *= 10;
printf("\tAfter Pass %d : ", pass + 1);
for (i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n");
}
}