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