diff options
author | millert <> | 2017-05-20 12:48:56 +0000 |
---|---|---|
committer | millert <> | 2017-05-20 12:48:56 +0000 |
commit | 78fdfc7b710f4922d56e067d044f7f7087c0872e (patch) | |
tree | 70cacb19950e1b5c46c66a865398a84d600761f5 /src | |
parent | abfa5942eee63f49119ddd016cb3f95b77b7c16b (diff) | |
download | openbsd-78fdfc7b710f4922d56e067d044f7f7087c0872e.tar.gz openbsd-78fdfc7b710f4922d56e067d044f7f7087c0872e.tar.bz2 openbsd-78fdfc7b710f4922d56e067d044f7f7087c0872e.zip |
Use David Musser's introsort algorithm to fall back to heapsort(3)
when the recursion depth reaches 2*lg(n + 1). This avoids quicksort's
quadratic behavior for pathological input without appreciably
changing the average run time.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/heapsort.c | 3 | ||||
-rw-r--r-- | src/lib/libc/stdlib/qsort.c | 56 |
2 files changed, 45 insertions, 14 deletions
diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c index 878567729e..f1db2205b0 100644 --- a/src/lib/libc/stdlib/heapsort.c +++ b/src/lib/libc/stdlib/heapsort.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: heapsort.c,v 1.10 2016/10/22 19:19:34 tb Exp $ */ | 1 | /* $OpenBSD: heapsort.c,v 1.11 2017/05/20 12:48:56 millert Exp $ */ |
2 | /*- | 2 | /*- |
3 | * Copyright (c) 1991, 1993 | 3 | * Copyright (c) 1991, 1993 |
4 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -172,3 +172,4 @@ heapsort(void *vbase, size_t nmemb, size_t size, | |||
172 | free(k); | 172 | free(k); |
173 | return (0); | 173 | return (0); |
174 | } | 174 | } |
175 | DEF_WEAK(heapsort); | ||
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c index c97979ef01..4fe54eb5ff 100644 --- a/src/lib/libc/stdlib/qsort.c +++ b/src/lib/libc/stdlib/qsort.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: qsort.c,v 1.15 2017/05/17 16:58:20 millert Exp $ */ | 1 | /* $OpenBSD: qsort.c,v 1.16 2017/05/20 12:48:56 millert Exp $ */ |
2 | /*- | 2 | /*- |
3 | * Copyright (c) 1992, 1993 | 3 | * Copyright (c) 1992, 1993 |
4 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -38,6 +38,18 @@ static __inline void swapfunc(char *, char *, size_t, int); | |||
38 | 38 | ||
39 | /* | 39 | /* |
40 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". | 40 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". |
41 | * | ||
42 | * This version differs from Bentley & McIlroy in the following ways: | ||
43 | * 1. The partition value is swapped into a[0] instead of being | ||
44 | * stored out of line. | ||
45 | * | ||
46 | * 2. It uses David Musser's introsort algorithm to fall back to | ||
47 | * heapsort(3) when the recursion depth reaches 2*lg(n + 1). | ||
48 | * This avoids quicksort's quadratic behavior for pathological | ||
49 | * input without appreciably changing the average run time. | ||
50 | * | ||
51 | * 3. Tail recursion is eliminated when sorting the larger of two | ||
52 | * subpartitions to save stack space. | ||
41 | */ | 53 | */ |
42 | #define swapcode(TYPE, parmi, parmj, n) { \ | 54 | #define swapcode(TYPE, parmi, parmj, n) { \ |
43 | size_t i = (n) / sizeof (TYPE); \ | 55 | size_t i = (n) / sizeof (TYPE); \ |
@@ -80,15 +92,20 @@ med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) | |||
80 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); | 92 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); |
81 | } | 93 | } |
82 | 94 | ||
83 | void | 95 | static void |
84 | qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) | 96 | introsort(char *a, size_t n, size_t es, size_t maxdepth, |
97 | int (*cmp)(const void *, const void *)) | ||
85 | { | 98 | { |
86 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | 99 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; |
87 | int cmp_result, swaptype; | 100 | int cmp_result, swaptype; |
88 | size_t d, r, s; | 101 | size_t r, s; |
89 | char *a = aa; | ||
90 | 102 | ||
91 | loop: SWAPINIT(a, es); | 103 | loop: if (maxdepth == 0) { |
104 | if (heapsort(a, n, es, cmp) == 0) | ||
105 | return; | ||
106 | } | ||
107 | maxdepth--; | ||
108 | SWAPINIT(a, es); | ||
92 | if (n < 7) { | 109 | if (n < 7) { |
93 | for (pm = a + es; pm < a + n * es; pm += es) | 110 | for (pm = a + es; pm < a + n * es; pm += es) |
94 | for (pl = pm; pl > a && cmp(pl - es, pl) > 0; | 111 | for (pl = pm; pl > a && cmp(pl - es, pl) > 0; |
@@ -101,16 +118,15 @@ loop: SWAPINIT(a, es); | |||
101 | pl = a; | 118 | pl = a; |
102 | pn = a + (n - 1) * es; | 119 | pn = a + (n - 1) * es; |
103 | if (n > 40) { | 120 | if (n > 40) { |
104 | d = (n / 8) * es; | 121 | s = (n / 8) * es; |
105 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | 122 | pl = med3(pl, pl + s, pl + 2 * s, cmp); |
106 | pm = med3(pm - d, pm, pm + d, cmp); | 123 | pm = med3(pm - s, pm, pm + s, cmp); |
107 | pn = med3(pn - 2 * d, pn - d, pn, cmp); | 124 | pn = med3(pn - 2 * s, pn - s, pn, cmp); |
108 | } | 125 | } |
109 | pm = med3(pl, pm, pn, cmp); | 126 | pm = med3(pl, pm, pn, cmp); |
110 | } | 127 | } |
111 | swap(a, pm); | 128 | swap(a, pm); |
112 | pa = pb = a + es; | 129 | pa = pb = a + es; |
113 | |||
114 | pc = pd = a + (n - 1) * es; | 130 | pc = pd = a + (n - 1) * es; |
115 | for (;;) { | 131 | for (;;) { |
116 | while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) { | 132 | while (pb <= pc && (cmp_result = cmp(pb, a)) <= 0) { |
@@ -149,7 +165,7 @@ loop: SWAPINIT(a, es); | |||
149 | /* Recurse for 1st side, iterate for 2nd side. */ | 165 | /* Recurse for 1st side, iterate for 2nd side. */ |
150 | if (s > es) { | 166 | if (s > es) { |
151 | if (r > es) | 167 | if (r > es) |
152 | qsort(a, r / es, es, cmp); | 168 | introsort(a, r / es, es, maxdepth, cmp); |
153 | a = pn - s; | 169 | a = pn - s; |
154 | n = s / es; | 170 | n = s / es; |
155 | goto loop; | 171 | goto loop; |
@@ -158,10 +174,24 @@ loop: SWAPINIT(a, es); | |||
158 | /* Recurse for 2nd side, iterate for 1st side. */ | 174 | /* Recurse for 2nd side, iterate for 1st side. */ |
159 | if (r > es) { | 175 | if (r > es) { |
160 | if (s > es) | 176 | if (s > es) |
161 | qsort(pn - s, s / es, es, cmp); | 177 | introsort(pn - s, s / es, es, maxdepth, cmp); |
162 | n = r / es; | 178 | n = r / es; |
163 | goto loop; | 179 | goto loop; |
164 | } | 180 | } |
165 | } | 181 | } |
166 | } | 182 | } |
183 | |||
184 | void | ||
185 | qsort(void *a, size_t n, size_t es, int (*cmp)(const void *, const void *)) | ||
186 | { | ||
187 | size_t i, maxdepth = 0; | ||
188 | |||
189 | /* Approximate 2*ceil(lg(n + 1)) */ | ||
190 | for (i = n; i > 0; i >>= 1) | ||
191 | maxdepth++; | ||
192 | maxdepth *= 2; | ||
193 | |||
194 | introsort(a, n, es, maxdepth, cmp); | ||
195 | } | ||
196 | |||
167 | DEF_STRONG(qsort); | 197 | DEF_STRONG(qsort); |