-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsort.cpp
More file actions
403 lines (360 loc) · 6.72 KB
/
sort.cpp
File metadata and controls
403 lines (360 loc) · 6.72 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright (C) 1999 Lucent Technologies */
/* From 'Programming Pearls' by Jon Bentley */
/* sort.cpp -- test and time sorting algorithms
Input lines: algnum n mod
Output lines: algnum n mod clicks nanosecs_per_elem
This is predominantly a C program; the only use of C++
sort function immediately below the include line.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// To change from C++ back to C, remove the following two lines
// and the call to sort in main
#include <algorithm>
using namespace std;
/* Data and supporting functions */
#define MAXN 10000000
typedef int DType;
DType realx[MAXN];
int *x = realx; /* allow x to shift for heaps */
int n;
void swap(int i, int j)
{ DType t = x[i];
x[i] = x[j];
x[j] = t;
}
int randint(int l, int u)
{ return l + (RAND_MAX*rand() + rand()) % (u-l+1);
}
/* LIBRARY QSORT */
int intcomp(int *x, int *y)
{ return *x - *y;
}
/* INSERTION SORTS */
/* Simplest insertion sort */
void isort1()
{ int i, j;
for (i = 1; i < n; i++)
for (j = i; j > 0 && x[j-1] > x[j]; j--)
swap(j-1, j);
}
/* Write swap function inline */
void isort2()
{ int i, j;
DType t;
for (i = 1; i < n; i++)
for (j = i; j > 0 && x[j-1] > x[j]; j--) {
t = x[j];
x[j] = x[j-1];
x[j-1] = t;
}
}
/* Move assignments to and from t out of loop */
void isort3()
{ int i, j;
DType t;
for (i = 1; i < n; i++) {
t = x[i];
for (j = i; j > 0 && x[j-1] > t; j--)
x[j] = x[j-1];
x[j] = t;
}
}
/* QUICKSORTS */
/* Simplest version, Lomuto partitioning */
void qsort1(int l, int u)
{ int i, m;
if (l >= u)
return;
m = l;
for (i = l+1; i <= u; i++)
if (x[i] < x[l])
swap(++m, i);
swap(l, m);
qsort1(l, m-1);
qsort1(m+1, u);
}
/* Sedgewick's version of Lomuto, with sentinel */
void qsort2(int l, int u)
{ int i, m;
if (l >= u)
return;
m = i = u+1;
do {
do i--; while (x[i] < x[l]);
swap(--m, i);
} while (i > l);
qsort2(l, m-1);
qsort2(m+1, u);
}
/* Two-way partitioning */
void qsort3(int l, int u)
{ int i, j;
DType t;
if (l >= u)
return;
t = x[l];
i = l;
j = u+1;
for (;;) {
do i++; while (i <= u && x[i] < t);
do j--; while (x[j] > t);
if (i > j)
break;
swap(i, j);
}
swap(l, j);
qsort3(l, j-1);
qsort3(j+1, u);
}
/* qsort3 + randomization + isort small subarrays + swap inline */
int cutoff = 50;
void qsort4(int l, int u)
{ int i, j;
DType t, temp;
if (u - l < cutoff)
return;
swap(l, randint(l, u));
t = x[l];
i = l;
j = u+1;
for (;;) {
do i++; while (i <= u && x[i] < t);
do j--; while (x[j] > t);
if (i > j)
break;
temp = x[i]; x[i] = x[j]; x[j] = temp;
}
swap(l, j);
qsort4(l, j-1);
qsort4(j+1, u);
}
/* selection */
void select1(int l, int u, int k)
{ int i, j;
DType t, temp;
if (l >= u)
return;
swap(l, randint(l, u));
t = x[l];
i = l;
j = u+1;
for (;;) {
do i++; while (i <= u && x[i] < t);
do j--; while (x[j] > t);
if (i > j)
break;
temp = x[i]; x[i] = x[j]; x[j] = temp;
}
swap(l, j);
if (j < k)
select1(j+1, u, k);
else if (j > k)
select1(l, j-1, k);
}
/* HEAP SORTS */
void siftup(int u)
{ int i, p;
i = u;
for (;;) {
if (i == 1)
break;
p = i / 2;
if (x[p] >= x[i])
break;
swap(p, i);
i = p;
}
}
void siftdown1(int l, int u)
{ int i, c;
i = l;
for (;;) {
c = 2*i;
if (c > u)
break;
if (c+1 <= u && x[c+1] > x[c])
c++;
if (x[i] > x[c])
break;
swap(i, c);
i = c;
}
}
void siftdown1b(int l, int u) /* More C-ish version of 1 */
{ int i, c;
for (i = l; (c = 2*i) <= u; i = c) {
if (c+1 <= u && x[c+1] > x[c])
c++;
if (x[i] > x[c])
break;
swap(i, c);
}
}
void hsort1()
{ int i;
x--;
for (i = 2; i <= n; i++)
siftup(i);
for (i = n; i >= 2; i--) {
swap(1, i);
siftdown1(1, i-1);
}
x++;
}
void hsort2()
{ int i;
x--;
for (i = n/2; i >= 1; i--)
siftdown1(i, n);
for (i = n; i >= 2; i--) {
swap(1, i);
siftdown1(1, i-1);
}
x++;
}
void siftdown3(int l, int u) /* push to bottom, then back up */
{ int i, c, p;
i = l;
for (;;) {
c = 2*i;
if (c > u)
break;
if (c+1 <= u && x[c+1] > x[c])
c++;
swap(i, c);
i = c;
}
for (;;) {
p = i/2;
if (p < l)
break;
if (x[p] > x[i])
break;
swap(p, i);
i = p;
}
}
void hsort3()
{ int i;
x--;
for (i = n/2; i >= 1; i--)
siftdown3(i, n);
for (i = n; i >= 2; i--) {
swap(1, i);
siftdown3(1, i-1);
}
x++;
}
void siftdown4(int l, int u) /* replace swap with assignments */
{ int i, c, p;
DType t;
t = x[l];
i = l;
for (;;) {
c = 2*i;
if (c > u)
break;
if (c+1 <= u && x[c+1] > x[c])
c++;
x[i] = x[c];
i = c;
}
x[i] = t;
for (;;) {
p = i/2;
if (p < l)
break;
if (x[p] > x[i])
break;
swap(p, i);
i = p;
}
}
void hsort4()
{ int i;
x--;
for (i = n/2; i >= 1; i--)
siftdown4(i, n);
for (i = n; i >= 2; i--) {
swap(1, i);
siftdown4(1, i-1);
}
x++;
}
/* Other Sorts -- Exercises in Column 11 */
void selsort() /* Selection sort */
{ int i, j;
for (i = 0; i < n-1; i++)
for (j = i; j < n; j++)
if (x[j] < x[i])
swap(i, j);
}
void shellsort()
{ int i, j, h;
for (h = 1; h < n; h = 3*h + 1)
;
for (;;) {
h /= 3;
if (h < 1) break;
for (i = h; i < n; i++) {
for (j = i; j >= h; j -= h) {
if (x[j-h] < x[j]) break;
swap(j-h, j);
}
}
}
}
/* SCAFFOLDING */
/* Timing */
void timedriver()
{ int i, k, algnum, mod, start, clicks;
while (scanf("%d %d %d", &algnum, &n, &mod) != EOF) {
if (mod <= 0)
mod = 10*n;
for (i = 0; i < n; i++)
x[i] = randint(0, mod-1);
k = n/2;
start = clock();
switch (algnum) {
case 11: qsort(x, n, sizeof(int), (int (__cdecl *)(const void *,const void *)) intcomp); break;
case 12: sort(x, x+n); break;
case 21: isort1(); break;
case 22: isort2(); break;
case 23: isort3(); break;
case 31: qsort1(0, n-1); break;
case 32: qsort2(0, n-1); break;
case 33: qsort3(0, n-1); break;
case 34: qsort4(0, n-1); isort3(); break;
case 41: select1(0, n-1, k); break;
case 51: hsort1(); break;
case 52: hsort2(); break;
case 53: hsort3(); break;
case 54: hsort4(); break;
case 61: selsort(); break;
case 62: shellsort(); break;
}
clicks = clock() - start;
if (algnum == 41) { /* Test selection */
for (i = 0; i < k; i++)
if (x[i] > x[k])
printf(" SELECT BUG i=%d\n", i);
for (i = k+1; i < n; i++)
if (x[i] < x[k])
printf(" SELECT BUG i=%d\n", i);
} else { /* Test sort */
for (i = 0; i < n-1; i++)
if (x[i] > x[i+1])
printf(" SORT BUG i=%d\n", i);
}
printf("%d\t%d\t%d\t%d\t%g\n",
algnum, n, mod, clicks,
1e9*clicks/((float) CLOCKS_PER_SEC*n));
}
}
/* Main */
int main()
{ timedriver();
return 0;
}