-
Notifications
You must be signed in to change notification settings - Fork 266
/
RadixSort.c
54 lines (47 loc) · 1.14 KB
/
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
#include <stdio.h>
void print_array(int array[], int size) {
printf("[ ");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("]\n");
}
int get_max(int array[], int size) {
int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > max)
max = array[i];
}
return max;
}
void radix_sort(int array[], int size) {
int i;
int semi_sorted[size];
int significant_digit = 1;
int largest_number = get_max(array, size);
while (largest_number / significant_digit > 0) {
int bucket[10] = {0};
for (i = 0; i < size; i++) {
bucket[(array[i] / significant_digit) % 10]++;
}
for (i = 1; i < 10; i++) {
bucket[i] += bucket[i - 1];
}
for (i = size - 1; i >= 0; i--) {
semi_sorted[--bucket[(array[i] / significant_digit) % 10]] = array[i];
}
for (i = 0; i < size; i++) {
array[i] = semi_sorted[i];
}
significant_digit *= 10;
}
}
int main() {
int array[10] = {45, 75, 89, 12, 34, 9, 67, 23, 90, 11};
printf("Unsorted Array\n");
print_array(array, 10);
radix_sort(array, 10);
printf("Sorted Array\n");
print_array(array, 10);
return 0;
}