-
Notifications
You must be signed in to change notification settings - Fork 64
/
sort.c
437 lines (354 loc) · 13.5 KB
/
sort.c
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*!
\file sort.c
\brief This file contains GKlib's various sorting routines
These routines are implemented using the GKSORT macro that is defined
in gk_qsort.h and is based on GNU's GLIBC qsort() implementation.
Additional sorting routines can be created using the same way that
these routines where defined.
\date Started 4/4/07
\author George
\version\verbatim $Id: sort.c 21050 2017-05-25 03:53:58Z karypis $ \endverbatim
*/
#include <GKlib.h>
/*************************************************************************/
/*! Sorts an array of chars in increasing order */
/*************************************************************************/
void gk_csorti(size_t n, char *base)
{
#define char_lt(a, b) ((*a) < (*b))
GK_MKQSORT(char, base, n, char_lt);
#undef char_lt
}
/*************************************************************************/
/*! Sorts an array of chars in decreasing order */
/*************************************************************************/
void gk_csortd(size_t n, char *base)
{
#define char_gt(a, b) ((*a) > (*b))
GK_MKQSORT(char, base, n, char_gt);
#undef char_gt
}
/*************************************************************************/
/*! Sorts an array of integers in increasing order */
/*************************************************************************/
void gk_isorti(size_t n, int *base)
{
#define int_lt(a, b) ((*a) < (*b))
GK_MKQSORT(int, base, n, int_lt);
#undef int_lt
}
/*************************************************************************/
/*! Sorts an array of integers in decreasing order */
/*************************************************************************/
void gk_isortd(size_t n, int *base)
{
#define int_gt(a, b) ((*a) > (*b))
GK_MKQSORT(int, base, n, int_gt);
#undef int_gt
}
/*************************************************************************/
/*! Sorts an array of integers in increasing order */
/*************************************************************************/
void gk_i32sorti(size_t n, int32_t *base)
{
#define int_lt(a, b) ((*a) < (*b))
GK_MKQSORT(int32_t, base, n, int_lt);
#undef int_lt
}
/*************************************************************************/
/*! Sorts an array of integers in decreasing order */
/*************************************************************************/
void gk_i32sortd(size_t n, int32_t *base)
{
#define int_gt(a, b) ((*a) > (*b))
GK_MKQSORT(int32_t, base, n, int_gt);
#undef int_gt
}
/*************************************************************************/
/*! Sorts an array of integers in increasing order */
/*************************************************************************/
void gk_i64sorti(size_t n, int64_t *base)
{
#define int_lt(a, b) ((*a) < (*b))
GK_MKQSORT(int64_t, base, n, int_lt);
#undef int_lt
}
/*************************************************************************/
/*! Sorts an array of integers in increasing order */
/*************************************************************************/
void gk_ui32sorti(size_t n, uint32_t *base)
{
#define int_lt(a, b) ((*a) < (*b))
GK_MKQSORT(uint32_t, base, n, int_lt);
#undef int_lt
}
/*************************************************************************/
/*! Sorts an array of integers in decreasing order */
/*************************************************************************/
void gk_ui32sortd(size_t n, uint32_t *base)
{
#define int_gt(a, b) ((*a) > (*b))
GK_MKQSORT(uint32_t, base, n, int_gt);
#undef int_gt
}
/*************************************************************************/
/*! Sorts an array of integers in increasing order */
/*************************************************************************/
void gk_ui64sorti(size_t n, uint64_t *base)
{
#define int_lt(a, b) ((*a) < (*b))
GK_MKQSORT(uint64_t, base, n, int_lt);
#undef int_lt
}
/*************************************************************************/
/*! Sorts an array of integers in decreasing order */
/*************************************************************************/
void gk_ui64sortd(size_t n, uint64_t *base)
{
#define int_gt(a, b) ((*a) > (*b))
GK_MKQSORT(uint64_t, base, n, int_gt);
#undef int_gt
}
/*************************************************************************/
/*! Sorts an array of integers in decreasing order */
/*************************************************************************/
void gk_i64sortd(size_t n, int64_t *base)
{
#define int_gt(a, b) ((*a) > (*b))
GK_MKQSORT(int64_t, base, n, int_gt);
#undef int_gt
}
/*************************************************************************/
/*! Sorts an array of floats in increasing order */
/*************************************************************************/
void gk_fsorti(size_t n, float *base)
{
#define float_lt(a, b) ((*a) < (*b))
GK_MKQSORT(float, base, n, float_lt);
#undef float_lt
}
/*************************************************************************/
/*! Sorts an array of floats in decreasing order */
/*************************************************************************/
void gk_fsortd(size_t n, float *base)
{
#define float_gt(a, b) ((*a) > (*b))
GK_MKQSORT(float, base, n, float_gt);
#undef float_gt
}
/*************************************************************************/
/*! Sorts an array of doubles in increasing order */
/*************************************************************************/
void gk_dsorti(size_t n, double *base)
{
#define double_lt(a, b) ((*a) < (*b))
GK_MKQSORT(double, base, n, double_lt);
#undef double_lt
}
/*************************************************************************/
/*! Sorts an array of doubles in decreasing order */
/*************************************************************************/
void gk_dsortd(size_t n, double *base)
{
#define double_gt(a, b) ((*a) > (*b))
GK_MKQSORT(double, base, n, double_gt);
#undef double_gt
}
/*************************************************************************/
/*! Sorts an array of gk_idx_t in increasing order */
/*************************************************************************/
void gk_idxsorti(size_t n, gk_idx_t *base)
{
#define idx_lt(a, b) ((*a) < (*b))
GK_MKQSORT(gk_idx_t, base, n, idx_lt);
#undef idx_lt
}
/*************************************************************************/
/*! Sorts an array of gk_idx_t in decreasing order */
/*************************************************************************/
void gk_idxsortd(size_t n, gk_idx_t *base)
{
#define idx_gt(a, b) ((*a) > (*b))
GK_MKQSORT(gk_idx_t, base, n, idx_gt);
#undef idx_gt
}
/*************************************************************************/
/*! Sorts an array of gk_ckv_t in increasing order */
/*************************************************************************/
void gk_ckvsorti(size_t n, gk_ckv_t *base)
{
#define ckey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_ckv_t, base, n, ckey_lt);
#undef ckey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_ckv_t in decreasing order */
/*************************************************************************/
void gk_ckvsortd(size_t n, gk_ckv_t *base)
{
#define ckey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_ckv_t, base, n, ckey_gt);
#undef ckey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_ikv_t in increasing order */
/*************************************************************************/
void gk_ikvsorti(size_t n, gk_ikv_t *base)
{
#define ikey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_ikv_t, base, n, ikey_lt);
#undef ikey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_ikv_t in decreasing order */
/*************************************************************************/
void gk_ikvsortd(size_t n, gk_ikv_t *base)
{
#define ikey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_ikv_t, base, n, ikey_gt);
#undef ikey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_i32kv_t in increasing order */
/*************************************************************************/
void gk_i32kvsorti(size_t n, gk_i32kv_t *base)
{
#define ikey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_i32kv_t, base, n, ikey_lt);
#undef ikey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_i32kv_t in decreasing order */
/*************************************************************************/
void gk_i32kvsortd(size_t n, gk_i32kv_t *base)
{
#define ikey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_i32kv_t, base, n, ikey_gt);
#undef ikey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_i64kv_t in increasing order */
/*************************************************************************/
void gk_i64kvsorti(size_t n, gk_i64kv_t *base)
{
#define ikey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_i64kv_t, base, n, ikey_lt);
#undef ikey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_i64kv_t in decreasing order */
/*************************************************************************/
void gk_i64kvsortd(size_t n, gk_i64kv_t *base)
{
#define ikey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_i64kv_t, base, n, ikey_gt);
#undef ikey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_zkv_t in increasing order */
/*************************************************************************/
void gk_zkvsorti(size_t n, gk_zkv_t *base)
{
#define zkey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_zkv_t, base, n, zkey_lt);
#undef zkey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_zkv_t in decreasing order */
/*************************************************************************/
void gk_zkvsortd(size_t n, gk_zkv_t *base)
{
#define zkey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_zkv_t, base, n, zkey_gt);
#undef zkey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_zukv_t in increasing order */
/*************************************************************************/
void gk_zukvsorti(size_t n, gk_zukv_t *base)
{
#define zukey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_zukv_t, base, n, zukey_lt);
#undef zukey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_zukv_t in decreasing order */
/*************************************************************************/
void gk_zukvsortd(size_t n, gk_zukv_t *base)
{
#define zukey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_zukv_t, base, n, zukey_gt);
#undef zukey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_fkv_t in increasing order */
/*************************************************************************/
void gk_fkvsorti(size_t n, gk_fkv_t *base)
{
#define fkey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_fkv_t, base, n, fkey_lt);
#undef fkey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_fkv_t in decreasing order */
/*************************************************************************/
void gk_fkvsortd(size_t n, gk_fkv_t *base)
{
#define fkey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_fkv_t, base, n, fkey_gt);
#undef fkey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_dkv_t in increasing order */
/*************************************************************************/
void gk_dkvsorti(size_t n, gk_dkv_t *base)
{
#define dkey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_dkv_t, base, n, dkey_lt);
#undef dkey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_fkv_t in decreasing order */
/*************************************************************************/
void gk_dkvsortd(size_t n, gk_dkv_t *base)
{
#define dkey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_dkv_t, base, n, dkey_gt);
#undef dkey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_skv_t in increasing order */
/*************************************************************************/
void gk_skvsorti(size_t n, gk_skv_t *base)
{
#define skey_lt(a, b) (strcmp((a)->key, (b)->key) < 0)
GK_MKQSORT(gk_skv_t, base, n, skey_lt);
#undef skey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_skv_t in decreasing order */
/*************************************************************************/
void gk_skvsortd(size_t n, gk_skv_t *base)
{
#define skey_gt(a, b) (strcmp((a)->key, (b)->key) > 0)
GK_MKQSORT(gk_skv_t, base, n, skey_gt);
#undef skey_gt
}
/*************************************************************************/
/*! Sorts an array of gk_idxkv_t in increasing order */
/*************************************************************************/
void gk_idxkvsorti(size_t n, gk_idxkv_t *base)
{
#define idxkey_lt(a, b) ((a)->key < (b)->key)
GK_MKQSORT(gk_idxkv_t, base, n, idxkey_lt);
#undef idxkey_lt
}
/*************************************************************************/
/*! Sorts an array of gk_idxkv_t in decreasing order */
/*************************************************************************/
void gk_idxkvsortd(size_t n, gk_idxkv_t *base)
{
#define idxkey_gt(a, b) ((a)->key > (b)->key)
GK_MKQSORT(gk_idxkv_t, base, n, idxkey_gt);
#undef idxkey_gt
}