-
Notifications
You must be signed in to change notification settings - Fork 3
/
radix_sort_u32.c
121 lines (101 loc) · 2.29 KB
/
radix_sort_u32.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define SWAP(a, b) do { __auto_type T = (a); a = b; b = T; } while (0)
struct sortrec {
uint32_t key;
const char *name;
};
static uint32_t key_of(const struct sortrec *rec) {
return rec->key;
}
static void radix_sort_u32(struct sortrec *arr, struct sortrec *aux, size_t n)
{
size_t cnt0[256] = { 0 };
size_t cnt1[256] = { 0 };
size_t cnt2[256] = { 0 };
size_t cnt3[256] = { 0 };
size_t i;
// Generate histograms
for (i = 0 ; i < n ; ++i) {
uint32_t k = key_of(arr + i);
uint8_t k0 = (k >> 0) & 0xFF;
uint8_t k1 = (k >> 8) & 0xFF;
uint8_t k2 = (k >> 16) & 0xFF;
uint8_t k3 = (k >> 24) & 0xFF;
++cnt0[k0];
++cnt1[k1];
++cnt2[k2];
++cnt3[k3];
}
// Calculate prefix sums.
size_t a0 = 0;
size_t a1 = 0;
size_t a2 = 0;
size_t a3 = 0;
for (int j = 0 ; j < 256 ; ++j) {
size_t b0 = cnt0[j];
size_t b1 = cnt1[j];
size_t b2 = cnt2[j];
size_t b3 = cnt3[j];
cnt0[j] = a0;
cnt1[j] = a1;
cnt2[j] = a2;
cnt3[j] = a3;
a0 += b0;
a1 += b1;
a2 += b2;
a3 += b3;
}
// Sort in four passes from LSB to MSB
for (i = 0 ; i < n ; ++i) {
uint32_t k = key_of(arr + i);
uint8_t k0 = (k >> 0) & 0xFF;
size_t dst = cnt0[k0]++;
aux[dst] = arr[i];
}
SWAP(arr, aux);
for (i = 0 ; i < n ; ++i) {
uint32_t k = key_of(arr + i);
uint8_t k1 = (k >> 8) & 0xFF;
size_t dst = cnt1[k1]++;
aux[dst] = arr[i];
}
SWAP(arr, aux);
for (i = 0 ; i < n ; ++i) {
uint32_t k = key_of(arr + i);
uint8_t k2 = (k >> 16) & 0xFF;
size_t dst = cnt2[k2]++;
aux[dst] = arr[i];
}
SWAP(arr, aux);
for (i = 0 ; i < n ; ++i) {
uint32_t k = key_of(arr + i);
uint8_t k3 = (k >> 24) & 0xFF;
size_t dst = cnt3[k3]++;
aux[dst] = arr[i];
}
}
static void print_array_rec(const struct sortrec *arr, size_t n) {
for (size_t i = 0 ; i < n ; ++i) {
printf("%08zx: %08x -> %s\n", i, key_of(&arr[i]), arr[i].name);
}
}
int main(int argc, char *argv[]) {
struct sortrec arr[] = {
{ 4255, "1st 4255" },
{ 45, "1st 45" },
{ 45, "2nd 45" },
{ 45, "3rd 45" },
{ 1, "1" },
{ 2, "2" },
{ 0xFFFFFFFF, "definitely last" },
{ 4255, "2nd 4255" },
};
size_t N = sizeof(arr)/sizeof(arr[0]);
struct sortrec *aux = malloc(sizeof(arr[0]) * N);
radix_sort_u32(arr, aux, N);
print_array_rec(arr, N);
free(aux);
return EXIT_SUCCESS;
}