-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_radix.c
More file actions
48 lines (37 loc) · 1.3 KB
/
10_radix.c
File metadata and controls
48 lines (37 loc) · 1.3 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
#include <stdio.h>
void countingSortByDigit(int arr[], int n, int exp) {
int output[n];
int count[10] = {0};
// Count occurrences of each digit
for (int i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
// Cumulative count
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build output(right to left for stability)
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / exp) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
for (int i = 0; i < n; i++)
arr[i] = output[i];
}
void radixSort(int arr[], int n) {
int max = arr[0];
for (int i=1; i<n; i++)
if (arr[i] > max) max = arr[i]; // We need to know the maximum number to know the number of digits
for (int exp = 1; max/exp > 0; exp *= 10)
countingSortByDigit(arr, n, exp); // Do counting sort for every digit. Note that instead of passing the digit number, exp is passed. exp is 10^i where i is the current digit number
}
int main() {
int a[] = {200, 63, 37, 820, 2, 67, 743, 294, 11};
int n = sizeof(a) / sizeof(int);
radixSort(a, n);
printf("After performing radix sort: \n");
for (int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}