-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix.c
More file actions
99 lines (72 loc) · 1.96 KB
/
radix.c
File metadata and controls
99 lines (72 loc) · 1.96 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* File: radix.c
*
* Copyright: 2020, Darren C. Atkinson
*
* Description: Read a sequence of non-negative integers from the
* standard input and sort then using radix sort. Each
* integer in the list is dropped into a bucket by its least
* significant digit. After all integers are placed in
* buckets, the buckets are copied back into the list and we
* repeat the process, but with the next most significant
* digit. After all digits have been processed, the list is
* sorted! Since the buckets need to preserve the order of
* insertion, we need to implement a queue. A list provides
* this functionality for us. The algorithm can be found at
* wikipedia.org/wiki/Radix_sort.
*/
# include <math.h>
# include <stdio.h>
# include <stdlib.h>
# include <assert.h>
# include "list.c"
# define r 10
/*
* Function: main
*
* Description: Driver function for the radix application.
*/
int main(void)
{
int i, x, niter, div, max, *p;
LIST *a, *lists[r];
max = 0;
a = createList();
for (i = 0; i < r; i ++)
lists[i] = createList();
/* Read in the numbers and record the maximum as we go along. */
while (scanf("%d", &x) == 1) {
if (x >= 0) {
p = malloc(sizeof(int));
assert(p != NULL);
*p = x;
addLast(a, p);
if (x > max)
max = x;
} else {
fprintf(stderr, "Sorry, only non-negative values allowed.\n");
exit(EXIT_FAILURE);
}
}
div = 1;
niter = ceil(log(max + 1) / log(r));
while (niter --) {
/* Move the numbers from the list to the buckets. */
while (numItems(a) > 0) {
p = removeFirst(a);
addLast(lists[*p / div % r], p);
}
/* Move the numbers from the buckets back into the list. */
for (i = 0; i < r; i ++)
while (numItems(lists[i]) > 0)
addLast(a, removeFirst(lists[i]));
div = div * r;
}
/* Print out the numbers. */
while (numItems(a) > 0) {
p = removeFirst(a);
printf("%d\n", *p);
free(p);
}
exit(EXIT_SUCCESS);
}