1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <msvcrt/stdlib.h>
3 #include <msvcrt/internal/tls.h>
6 * Copyright (c) 1980, 1983 The Regents of the University of California.
9 * Redistribution and use in source and binary forms are permitted
10 * provided that: (1) source distributions retain this entire copyright
11 * notice and comment, and (2) distributions including binaries display
12 * the following acknowledgement: ``This product includes software
13 * developed by the University of California, Berkeley and its contributors''
14 * in the documentation or other materials provided with the distribution
15 * and in all advertising materials mentioning features or use of this
16 * software. Neither the name of the University nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 * Our own version of the system qsort routine which is faster by an average
27 * of 25%, with lows and highs of 10% and 50%.
28 * The THRESHold below is the insertion sort threshold, and has been adjusted
29 * for records of size 48 bytes.
30 * The MTHREShold is where we stop finding a better median.
33 #define THRESH 4 /* threshold for insertion */
34 #define MTHRESH 6 /* threshold for median */
39 * First, find the median element, and put that one in the first place as the
40 * discriminator. (This "median" is just the median of the first, last and
41 * middle elements). (Using this median instead of the first element is a big
42 * win). Then, the usual partitioning/swapping, followed by moving the
43 * discriminator into the right place. Then, figure out the sizes of the two
44 * partions, do the smaller one recursively and the larger one via a repeat of
45 * this code. Stopping when there are less than THRESH elements in a partition
46 * and cleaning up with an insertion sort (in our caller) is a huge win.
47 * All data swaps are done in-line, which is space-losing but time-saving.
48 * (And there are only three places where this is done).
52 qst(PTHREADDATA pThreadData, char *base, char *max)
60 * At the top here, lo is the number of characters of elements in the
61 * current partition. (Which should be max - base).
62 * Find the median of the first, last, and middle element and make
63 * that the middle element. Set j to largest of first and middle.
64 * If max is larger than that guy, then it's that guy, else compare
65 * max with loser of first and take larger. Things are set up to
66 * prefer the middle, then the first in case of ties.
68 lo = max - base; /* number of elements as chars */
70 mid = i = base + pThreadData->qsz * ((lo / pThreadData->qsz) >> 1);
71 if (lo >= pThreadData->mthresh)
73 j = (pThreadData->qcmp((jj = base), i) > 0 ? jj : i);
74 if (pThreadData->qcmp(j, (tmp = max - pThreadData->qsz)) > 0)
76 /* switch to first loser */
77 j = (j == jj ? i : jj);
78 if (pThreadData->qcmp(j, tmp) < 0)
83 ii = pThreadData->qsz;
92 * Semi-standard quicksort partitioning/swapping
94 for (i = base, j = max - pThreadData->qsz; ; )
96 while (i < mid && pThreadData->qcmp(i, mid) <= 0)
97 i += pThreadData->qsz;
100 if (pThreadData->qcmp(mid, j) <= 0)
102 j -= pThreadData->qsz;
105 tmp = i + pThreadData->qsz; /* value of i after swap */
108 /* j <-> mid, new mid is j */
115 j -= pThreadData->qsz;
125 /* i <-> mid, new mid is i */
127 tmp = mid = i; /* value of i after swap */
128 j -= pThreadData->qsz;
131 ii = pThreadData->qsz;
140 * Look at sizes of the two partitions, do the smaller
141 * one first by recursion, then do the larger one by
142 * making sure lo is its size, base and max are update
143 * correctly, and branching back. But only repeat
144 * (recursively or by branching) if the partition is
145 * of at least size THRESH.
147 i = (j = mid) + pThreadData->qsz;
148 if ((lo = j - base) <= (hi = max - i))
150 if (lo >= pThreadData->thresh)
151 qst(pThreadData, base, j);
157 if (hi >= pThreadData->thresh)
158 qst(pThreadData, i, max);
161 } while (lo >= pThreadData->thresh);
166 * First, set up some global parameters for qst to share. Then, quicksort
167 * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
171 qsort(const void *base0, size_t n, size_t size, _pfunccmp_t compar)
173 PTHREADDATA pThreadData;
174 char *base = (char *)base0;
175 char c, *i, *j, *lo, *hi;
181 pThreadData = GetThreadData();
183 pThreadData->qsz = size;
184 pThreadData->qcmp = compar;
185 pThreadData->thresh = pThreadData->qsz * THRESH;
186 pThreadData->mthresh = pThreadData->qsz * MTHRESH;
187 max = base + n * pThreadData->qsz;
190 qst(pThreadData, base, max);
191 hi = base + pThreadData->thresh;
198 * First put smallest element, which must be in the first THRESH, in
199 * the first position as a sentinel. This is done just by searching
200 * the first THRESH elements (or the first n if n < THRESH), finding
201 * the min, and swapping it into the first position.
203 for (j = lo = base; (lo += pThreadData->qsz) < hi; )
204 if (pThreadData->qcmp(j, lo) > 0)
208 /* swap j into place */
209 for (i = base, hi = base + pThreadData->qsz; i < hi; )
217 * With our sentinel in place, we now run the following hyper-fast
218 * insertion sort. For each remaining element, min, from [1] to [n-1],
219 * set hi to the index of the element AFTER which this one goes.
220 * Then, do the standard insertion sort shift on a character at a time
221 * basis for each element in the frob.
223 for (min = base; (hi = min += pThreadData->qsz) < max; )
225 while (pThreadData->qcmp(hi -= pThreadData->qsz, min) > 0)
227 if ((hi += pThreadData->qsz) != min) {
228 for (lo = min + pThreadData->qsz; --lo >= min; )
231 for (i = j = lo; (j -= pThreadData->qsz) >= hi; i = j)