-
Notifications
You must be signed in to change notification settings - Fork 39
/
radix.h
112 lines (96 loc) · 2.6 KB
/
radix.h
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
#include <stdlib.h>
// complicated expression better fits as macro (or inline in C++)
#define ByteOf(x) (((x) >> bitsOffset) & 0xff)
static void radix (short bitsOffset, int N, int *source, int *dest)
{
// suppressed the need for index as it is reported in count
int count[256];
// added temp variables to simplify writing, understanding and compiler optimization job
// most of them will be allocated as registers
int *sp, *cp, s, c, i;
// faster than MemSet
cp = count;
for (i = 256; i > 0; --i, ++cp)
*cp = 0;
// count occurences of every byte value
sp = source;
for (i = N; i > 0; --i, ++sp) {
cp = count + ByteOf (*sp);
++(*cp);
}
// transform count into index by summing elements and storing into same array
s = 0;
cp = count;
for (i = 256; i > 0; --i, ++cp) {
c = *cp;
*cp = s;
s += c;
}
// fill dest with the right values in the right place
sp = source;
for (i = N; i > 0; --i, ++sp) {
cp = count + ByteOf (*sp);
dest[*cp] = *sp;
++(*cp);
}
}
// complicated expression better fits as macro (or inline in C++)
/*#define ByteOf(x) (((x) >> bitsOffset) & 0xff)
// replaced byte with bitsOffset to avoid *8 operation in loop
static void radix (short bitsOffset, int N, int *source, int *dest)
{
// suppressed the need for index as it is reported in count
int count[256];
// added temp variables to simplify writing, understanding and compiler optimization job
// most of them will be allocated as registers
int *cp, *sp, s, c, i;
// faster than MemSet
cp = count;
for (i = 256; i > 0; --i, ++cp)
*cp = 0;
// count occurences of every byte value
sp = source;
for (i = N; i > 0; --i, ++sp) {
cp = count + ByteOf (*sp);
++(*cp);
}
// transform count into index by summing elements and storing into same array
s = 0;
cp = count;
for (i = 256; i > 0; --i, ++cp) {
c = *cp;
*cp = s;
s += c;
}
// fill dest with the right values in the right place
sp = source;
for (i = N; i > 0; --i, ++sp) {
s = *sp;
cp = count + ByteOf (s);
dest[*cp] = s;
++(*cp);
}
}*/
static void radix_sort (int *source, int N)
{
// allocate heap memory to avoid the need of additional parameter
int *temp = malloc (N * sizeof (int));
radix (0, N, source, temp);
radix (8, N, temp, source);
radix (16, N, source, temp);
radix (24, N, temp, source);
free (temp);
}
/*
static int check_order (int *data, int N)
{
// only signal errors if any (should not be)
--N;
for ( ; N > 0; --N, ++data)
if (data[0] > data[1]) {
printf("%d %d",data[0],data[1]);
return N;
}
return -1;
}
*/