-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix_sort.c
79 lines (66 loc) · 1.38 KB
/
radix_sort.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
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
#include <stdio.h>
void printArray(int *arr, int n)
{
int i;
for(i=0; i<n; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
int findMax(int *A, int n)
{
int i;
int max = A[0];
for(i=1; i<n; i++){
if(A[i]>A[i-1])
max = A[i];
}
return max;
}
void countingSort(int *A, int n, int digit)
{
int i;
int decimal = 10;
int C[10];
for(i=0; i<10; i++){
C[i] = 0;
}
// Count only the least_significant_digit
// decimal is constant, always 10
// Ex: (250/1)%10 = 0
// Ex: (250/10)%10 = 5
for(i=0; i<n; i++){
C[(A[i]/digit)%decimal]++;
}
for(i=1; i<10; i++){
C[i] += C[i-1];
}
int B[n];
for(i=n-1; i>=0; i--){
B[--C[(A[i]/digit)%decimal]] = A[i];
}
for(i=0; i<n; i++){
A[i] = B[i];
}
}
void radixSort(int *A, int n)
{
int max_value = findMax(A,n);
int least_significant_digit = 1;
// Operate on the least_significant_digit first
// Ex: 250/1 = 250 hence 0
// next iteration 250/10 = 25, hence 5
while(max_value/least_significant_digit > 0){
countingSort(A,n,least_significant_digit);
least_significant_digit *= 10;
}
}
int main()
{
int arr[5] = {10,4,11,5,3};
int n = sizeof(arr)/sizeof(arr[0]);
printArray(arr, n);
radixSort(arr, n);
printArray(arr, n);
return 0;
}