forked from CosteaPaul/qaTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix.h
68 lines (57 loc) · 1.54 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
#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);
}
}
static void radix_sort (int *source, int N)
{
// allocate heap memory to avoid the need of additional parameter
int *temp = (int*)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;
}