1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/types.h>
19#include <exports.h>
20
21void qsort(void *base,
22 size_t nel,
23 size_t width,
24 int (*comp)(const void *, const void *))
25{
26 size_t wgap, i, j, k;
27 char tmp;
28
29 if ((nel > 1) && (width > 0)) {
30 assert(nel <= ((size_t)(-1)) / width);
31 wgap = 0;
32 do {
33 wgap = 3 * wgap + 1;
34 } while (wgap < (nel-1)/3);
35
36
37 wgap *= width;
38 nel *= width;
39 do {
40 i = wgap;
41 do {
42 j = i;
43 do {
44 register char *a;
45 register char *b;
46
47 j -= wgap;
48 a = j + ((char *)base);
49 b = a + wgap;
50 if ((*comp)(a, b) <= 0) {
51 break;
52 }
53 k = width;
54 do {
55 tmp = *a;
56 *a++ = *b;
57 *b++ = tmp;
58 } while (--k);
59 } while (j >= wgap);
60 i += width;
61 } while (i < nel);
62 wgap = (wgap - width)/3;
63 } while (wgap);
64 }
65}
66
67int strcmp_compar(const void *p1, const void *p2)
68{
69 return strcmp(*(const char **)p1, *(const char **)p2);
70}
71