-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradixsort.cc
279 lines (262 loc) · 6.99 KB
/
radixsort.cc
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <iostream>
#include <algorithm>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#include <strings.h>
//#include <sys/time.h>
typedef struct list {
struct list *next;
unsigned char *data;
} list;
namespace first_simple{
list *rsort(list *a, int b, list *sequel)
{
#define ended(a, b) b>0 && a->data[b-1]==0
#define append(s, a) tmp=a; while(tmp->next) tmp=tmp->next; tmp->next=s
#define deal(a, p) tmp = a->next, a->next = p, p = a, a = tmp
list *pile[256], *tmp;
int i;
if(a == 0)
return sequel;
if(ended(a, b)) { /* pile finished */
append(sequel, a);
return a;
}
for(i = 256; --i >= 0; ) /* split */
pile[i] = 0;
while(a)
deal(a, pile[a->data[b]]);
for(i = 256; --i >= 0; ) /* recur on each pile */
sequel = rsort(pile[i], b+1, sequel);
return sequel;
}
}
namespace improved{
list* rsort(list* a)
{
#define SIZE 256
#define singleton(a) a->next == 0
#define push(a, b) sp->sa = a, (sp++)->sb = b
#define pop(a, b) a = (--sp)->sa, b = sp->sb
#define stackempty() (sp <= stack)
struct { list *sa; int sb; } stack[SIZE], *sp = stack;
static list *pile[256];
list *tmp, *sequel = 0;
int i, b;
if(a)
push(a, 0);
while(!stackempty()) {
pop(a, b);
if(singleton(a) || ended(a, b)) { /* pile finished */
append(sequel, a);
sequel = a;
continue;
}
while(a) /* split */
deal(a, pile[a->data[b]]);
for(i = 0; i<256; i++) /* stack the pieces */
if(pile[i]) {
push(pile[i], b+1);
pile[i] = 0;
}
}
return sequel;
}
}
namespace twoArray{
typedef unsigned char *cString;
void rsort(cString *a, int n)
{
#define SIZE 256
#define push2(a, n, b) sp->sa = a, sp->sn = n, (sp++)->sb = b
#define pop2(a, n, b) a = (--sp)->sa, n = sp->sn, b = sp->sb
#define stackempty() (sp <= stack)
#define splittable(c) c > 0 && count[c] > 1
struct { cString *sa; int sn, sb; } stack[SIZE], *sp = stack;
cString *pile[256], *ak, *ta;
static int count[256];
int i, b;
ta = (cString*)malloc(n*sizeof(cString));
push2(a, n, 0);
while (!stackempty()) {
pop2(a, n, b);
for(i = n; --i >= 0; ) /* tally */
count[a[i][b]]++;
for(ak = a, i = 0; i < 256; i++) { /* find places */
if(splittable(i))
push2(ak, count[i], b+1);
pile[i] = ak += count[i];
count[i] = 0;
}
for(i = n; --i >= 0; ) /* move to temp */
ta[i] = a[i];
for(i = n; --i >= 0; ) /* move home */
*--pile[ta[i][b]] = ta[i];
}
free(ta);
}
}
namespace american_flag{
#ifdef SIZE
#undef SIZE
#endif // SIZE
#ifdef push
#undef push
#endif // push
#ifdef pop
#undef pop
#endif // pop
enum { SIZE = 510, THRESHOLD = 16 };
typedef unsigned char *cString;
typedef struct { cString *sa; int sn, si; } stack_t;
void simplesort(cString*, int, int){}
static void rsorta(cString *a, int n ,int b)
{
#define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
#define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
#define stackempty() (sp <= stack)
#define swap(p, q, r) r = p, p = q, q = r
stack_t stack[SIZE], *sp = stack, stmp, *oldsp, *bigsp;
cString *pile[256], *ak, *an, r, t;
static int count[256], cmin, nc;
int *cp, c, cmax/*, b = 0*/;
push(a, n, b);
while(!stackempty()) {
pop(a, n, b);
if(n < THRESHOLD) { /* divert */
simplesort(a, n, b);
continue;
}
an = a + n;
if(nc == 0) { /* untallied? */
cmin = 255; /* tally */
for(ak = a; ak < an; ) {
c = (*ak++)[b];
if(++count[c] == 1 && c > 0) {
if(c < cmin) cmin = c;
nc++;
}
}
if(sp+nc > stack+SIZE) { /* stack overflow */
rsorta(a, n, b);
continue;
}
}
oldsp = bigsp = sp, c = 2; /* logarithmic stack */
pile[0] = ak = a+count[cmax=0]; /* find places */
for(cp = count+cmin; nc > 0; cp++, nc--) {
while(*cp == 0) cp++;
if(*cp > 1) {
if(*cp > c) c = *cp, bigsp = sp;
push(ak, *cp, b+1);
}
pile[cmax = cp-count] = ak += *cp;
}
swap(*oldsp, *bigsp, stmp);
an -= count[cmax]; /* permute home */
count[cmax] = 0;
for(ak = a; ak < an; ak += count[c], count[c] = 0) {
r = *ak;
while(--pile[c = r[b]] > ak)
swap(*pile[c], r, t);
*ak = r;
} /* here nc = count[...] = 0 */
}
}
void rsort(cString *a, int n) { rsorta(a, n, 0); }
}
void insertion_sort(int *array, int offset, int end) {
int x, y, temp;
for (x=offset; x<end; ++x) {
for (y=x; y>offset && array[y-1]>array[y]; y--) {
temp = array[y];
array[y] = array[y-1];
array[y-1] = temp;
}
}
}
void radix_sort(int *data, int offset, int end, int shift) {
int x, y, value, temp;
int last[256] = { 0 }, pointer[256];
for (x=offset; x<end; ++x) {
++last[(data[x] >> shift) & 0xFF];
}
last[0] += offset;
pointer[0] = offset;
for (x=1; x<256; ++x) {
pointer[x] = last[x-1];
last[x] += last[x-1];
}
for (x=0; x<256; ++x) {
while (pointer[x] != last[x]) {
value = data[pointer[x]];
y = (value >> shift) & 0xFF;
while (x != y) {
temp = data[pointer[y]];
data[pointer[y]++] = value;
value = temp;
y = (value >> shift) & 0xFF;
}
data[pointer[x]++] = value;
}
}
if (shift > 0) {
shift -= 8;
for (x=0; x<256; ++x) {
temp = x > 0 ? pointer[x] - pointer[x-1] : pointer[0] - offset;
if (temp > 64) {
radix_sort(data, pointer[x] - temp, pointer[x], shift);
} else if (temp > 1) {
// std::sort(array + (pointer[x] - temp), array + pointer[x]);
insertion_sort(data, pointer[x] - temp, pointer[x]);
}
}
}
}
int intcmp(const void *aa, const void *bb)
{
const int *a = (int *)aa, *b = (int *)bb;
return (*a < *b) ? -1 : (*a > *b);
}
int main(int argc, char **argv) {
if (sizeof(int) != 4) {
std::cerr << "Ooops. sizeof(int) != 4\n";
return 1111;
}
int N;
if (argc != 2 || sscanf(argv[1], "%d", &N) == -1) {
std::cerr << "n missing\n";
return 111;
}
int *array = (int *)malloc(sizeof(int) * N);
for (int i=2; i<3; ++i) {
int n = N;
srand(1);
while (n --> 0) {
array[n] = rand();
}
clock_t t1 = clock();
switch(i) {
case 0: std::sort(array, array+N); break;
case 1: qsort(array, N, sizeof(int), intcmp); break;
case 2: radix_sort(array, 0, N, 24); break;
}
clock_t t2 = clock();
n = N - 1;
while (n --> 0) {
if (array[n] > array[n+1]) {
std::cerr << "sorting failed\n";
return -1;
}
}
double mtime = (double)(t2-t1)/CLOCKS_PER_SEC;
switch(i) {
case 0: std::cout << "std::sort " << mtime << "\n"; break;
case 1: std::cout << "qsort " << mtime << "\n"; break;
case 2: std::cout << "radix_sort " << mtime << "\n"; break;
}
}
return 0;
}