-
Notifications
You must be signed in to change notification settings - Fork 45
/
radix_sort.cpp
30 lines (30 loc) · 946 Bytes
/
radix_sort.cpp
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
// works in-place, O(n*d), swap needs to be adapted
// could contain a bug! test against sort first!
//
// len = number of digits
// getdig(x, 0) should get the most significant digit, get(x, len - 1) the lsd etc.
const int max_val=256;
template<typename T, typename F>
void radix_sort_msb(T& seq, F getdig, int len, int l, int r, int base = 0) {
if (r-l <= 20) {
sort(&seq[l],&seq[r]); // even better: use insertion sort
return;
}
if (base == len || r-l <= 1) return;
int start[max_val+1] = {0}, pos[max_val+1] = {0};
start[0] = l;
rep(i,l,r) start[getdig(seq[i], base)+1]++;
rep(i,1,max_val+1) start[i] += start[i-1], pos[i] = start[i];
rep(c,0,max_val) {
rep(i,start[c], start[c+1]) {
for(;;) {
int d = getdig(seq[i], base);
if (d==c) break;
int& j = pos[d];
swap(seq[i], seq[j]);
j++;
}
}
radix_sort_msb(seq,getdig,len,start[c],start[c+1],base+1);
}
}