-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradixSort.c
60 lines (49 loc) · 925 Bytes
/
radixSort.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
/*
Karan Chawla
Algorithm: Radix Sort
*/
#include <stdio.h>
int getMax(int *A, int size){
int max = A[0];
for (int i=0;i<size;i++){
if (A[i]>max){
max = A[i];
}
}
return max;
}
void countingSort(int *A, int size, int e){
int output[size];
int i, count[10] = {0};
for(int i=0;i<size;i++){
count[(A[i]/e)%10]++;
}
for(int i=1;i<size;i++){
count[i] += count[i-1];
}
for (int i=0;i<size;i++){
output[count[(A[i]/e)%10 -1]] = A[i];
count[(A[i]/e)%10]--;
}
for (int i=0;i<size;i++){
A[i] = output[i];
}
}
void radixSort(int *A, int size){
int m = getMax(A, size);
for(int e=1; m/e > 0; e*=10){
countingSort(A, size, e);
}
}
void printArray(int *A, int size){
for (int i=0;i<size;i++){
printf("%d\n", *(A+i));
}
}
int main(void){
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
int n = sizeof(arr)/sizeof(arr[0]);
radixSort(arr,n);
printArray(arr,n);
return 0;
}