-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathcountingsort
58 lines (44 loc) · 1.09 KB
/
countingsort
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
#include <stdio.h>
#include <string.h>
#define WORD_SIZE 10
#define POSITIONS 100
struct word {
int position;
char term[WORD_SIZE + 1];
};
void counting_sort(char *sorted[], struct word words[], size_t size) {
int buckets[POSITIONS];
int i;
for (i = 0; i < POSITIONS; i++) {
buckets[i] = 0;
}
for (i = 0; i < size; i++) {
buckets[words[i].position]++;
}
for (i = 1; i < POSITIONS; i++) {
buckets[i] += buckets[i - 1];
}
for (i = size - 1; i >= 0; i--) {
sorted[--buckets[words[i].position]] = words[i].term;
}
}
int main() {
int n, i, max;
char garbage[WORD_SIZE + 1];
scanf("%d", &n);
struct word words[n];
char *sorted[n];
max = n / 2;
for (i = 0; i < max; i++) {
scanf("%d %10s", &words[i].position, garbage);
strcpy(words[i].term, "-");
}
for (; i < n; i++) {
scanf("%d %10s", &words[i].position, words[i].term);
}
counting_sort(sorted, words, n);
for (i = 0; i < n; i++) {
printf("%s ", sorted[i]);
}
return 0;
}