-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix-sort.c
More file actions
79 lines (60 loc) · 1.46 KB
/
radix-sort.c
File metadata and controls
79 lines (60 loc) · 1.46 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
#include <stdio.h>
#define MAX_LEN 100
static int get_max(int array[], int n)
{
int max = array[0];
for (int i = 1; i < n; i++)
if (array[i] > max)
max = array[i];
return max;
}
static void count_sort(int array[], int n, int exp)
{
int output[n];
int i, count[10] = {0};
for (i = 0; i < 10; i++) {
count[i] = 0;
}
for (i = 0; i < n; i++) {
count[(array[i] / exp) % 10]++;
}
for (i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
output[count[(array[i] / exp) % 10] - 1] = array[i];
count[(array[i] / exp) % 10]--;
}
for (i = 0; i < n; i++)
array[i] = output[i];
}
static void sort(int array[], int n)
{
int m = get_max(array, n);
for (int exp = 1; m / exp > 0; exp *= 10)
count_sort(array, n, exp);
}
static void print_array(int *array, int len)
{
int i;
for (i = 0; i < len; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int array[MAX_LEN], len, i;
printf("What's the length of the array? Maximum lenght is %d\n", MAX_LEN);
scanf("%d", &len);
printf("Gimme the %d elements\n", len);
for (i = 0; i < len; i++) {
scanf("%d", &array[i]);
}
printf("Nonsorted array: ");
print_array(array, len);
sort(array, len);
printf("Sorted array: ");
print_array(array, len);
return 0;
}