1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
2 #include <crtdll/stdlib.h>
5 * Copyright (c) 1980, 1983 The Regents of the University of California.
8 * Redistribution and use in source and binary forms are permitted
9 * provided that: (1) source distributions retain this entire copyright
10 * notice and comment, and (2) distributions including binaries display
11 * the following acknowledgement: ``This product includes software
12 * developed by the University of California, Berkeley and its contributors''
13 * in the documentation or other materials provided with the distribution
14 * and in all advertising materials mentioning features or use of this
15 * software. Neither the name of the University nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25 * Our own version of the system qsort routine which is faster by an average
26 * of 25%, with lows and highs of 10% and 50%.
27 * The THRESHold below is the insertion sort threshold, and has been adjusted
28 * for records of size 48 bytes.
29 * The MTHREShold is where we stop finding a better median.
32 #define THRESH 4 /* threshold for insertion */
33 #define MTHRESH 6 /* threshold for median */
35 static int (*qcmp)(const void *, const void *); /* the comparison routine */
36 static int qsz; /* size of each record */
37 static int thresh; /* THRESHold in chars */
38 static int mthresh; /* MTHRESHold in chars */
43 * First, find the median element, and put that one in the first place as the
44 * discriminator. (This "median" is just the median of the first, last and
45 * middle elements). (Using this median instead of the first element is a big
46 * win). Then, the usual partitioning/swapping, followed by moving the
47 * discriminator into the right place. Then, figure out the sizes of the two
48 * partions, do the smaller one recursively and the larger one via a repeat of
49 * this code. Stopping when there are less than THRESH elements in a partition
50 * and cleaning up with an insertion sort (in our caller) is a huge win.
51 * All data swaps are done in-line, which is space-losing but time-saving.
52 * (And there are only three places where this is done).
56 qst(char *base, char *max)
64 * At the top here, lo is the number of characters of elements in the
65 * current partition. (Which should be max - base).
66 * Find the median of the first, last, and middle element and make
67 * that the middle element. Set j to largest of first and middle.
68 * If max is larger than that guy, then it's that guy, else compare
69 * max with loser of first and take larger. Things are set up to
70 * prefer the middle, then the first in case of ties.
72 lo = max - base; /* number of elements as chars */
74 mid = i = base + qsz * ((lo / qsz) >> 1);
77 j = (qcmp((jj = base), i) > 0 ? jj : i);
78 if (qcmp(j, (tmp = max - qsz)) > 0)
80 /* switch to first loser */
81 j = (j == jj ? i : jj);
96 * Semi-standard quicksort partitioning/swapping
98 for (i = base, j = max - qsz; ; )
100 while (i < mid && qcmp(i, mid) <= 0)
104 if (qcmp(mid, j) <= 0)
109 tmp = i + qsz; /* value of i after swap */
112 /* j <-> mid, new mid is j */
129 /* i <-> mid, new mid is i */
131 tmp = mid = i; /* value of i after swap */
144 * Look at sizes of the two partitions, do the smaller
145 * one first by recursion, then do the larger one by
146 * making sure lo is its size, base and max are update
147 * correctly, and branching back. But only repeat
148 * (recursively or by branching) if the partition is
149 * of at least size THRESH.
152 if ((lo = j - base) <= (hi = max - i))
165 } while (lo >= thresh);
170 * First, set up some global parameters for qst to share. Then, quicksort
171 * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
175 qsort(const void *base0, size_t n, size_t size, _pfunccmp_t compar)
177 char *base = (char *)base0;
178 char c, *i, *j, *lo, *hi;
185 thresh = qsz * THRESH;
186 mthresh = qsz * MTHRESH;
187 max = base + n * qsz;
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 += qsz) < hi; )
208 /* swap j into place */
209 for (i = base, hi = base + 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 += qsz) < max; )
225 while (qcmp(hi -= qsz, min) > 0)
227 if ((hi += qsz) != min) {
228 for (lo = min + qsz; --lo >= min; )
231 for (i = j = lo; (j -= qsz) >= hi; i = j)