1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
7 * Copyright (c) 1980, 1983 The Regents of the University of California.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that: (1) source distributions retain this entire copyright
12 * notice and comment, and (2) distributions including binaries display
13 * the following acknowledgement: ``This product includes software
14 * developed by the University of California, Berkeley and its contributors''
15 * in the documentation or other materials provided with the distribution
16 * and in all advertising materials mentioning features or use of this
17 * software. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 * Our own version of the system qsort routine which is faster by an average
28 * of 25%, with lows and highs of 10% and 50%.
29 * The THRESHold below is the insertion sort threshold, and has been adjusted
30 * for records of size 48 bytes.
31 * The MTHREShold is where we stop finding a better median.
34 #define THRESH 4 /* threshold for insertion */
35 #define MTHRESH 6 /* threshold for median */
40 * First, find the median element, and put that one in the first place as the
41 * discriminator. (This "median" is just the median of the first, last and
42 * middle elements). (Using this median instead of the first element is a big
43 * win). Then, the usual partitioning/swapping, followed by moving the
44 * discriminator into the right place. Then, figure out the sizes of the two
45 * partions, do the smaller one recursively and the larger one via a repeat of
46 * this code. Stopping when there are less than THRESH elements in a partition
47 * and cleaning up with an insertion sort (in our caller) is a huge win.
48 * All data swaps are done in-line, which is space-losing but time-saving.
49 * (And there are only three places where this is done).
53 qst(size_t size
, int (__cdecl
*compar
)(const void*, const void*), char *base
, char *max
)
62 thresh
= size
* THRESH
;
63 mthresh
= size
* MTHRESH
;
66 * At the top here, lo is the number of characters of elements in the
67 * current partition. (Which should be max - base).
68 * Find the median of the first, last, and middle element and make
69 * that the middle element. Set j to largest of first and middle.
70 * If max is larger than that guy, then it's that guy, else compare
71 * max with loser of first and take larger. Things are set up to
72 * prefer the middle, then the first in case of ties.
74 lo
= max
- base
; /* number of elements as chars */
76 mid
= i
= base
+ size
* ((lo
/ size
) >> 1);
79 j
= (compar((jj
= base
), i
) > 0 ? jj
: i
);
80 if (compar(j
, (tmp
= max
- size
)) > 0)
82 /* switch to first loser */
83 j
= (j
== jj
? i
: jj
);
84 if (compar(j
, tmp
) < 0)
98 * Semi-standard quicksort partitioning/swapping
100 for (i
= base
, j
= max
- size
; ; )
102 while (i
< mid
&& compar(i
, mid
) <= 0)
106 if (compar(mid
, j
) <= 0)
111 tmp
= i
+ size
; /* value of i after swap */
114 /* j <-> mid, new mid is j */
131 /* i <-> mid, new mid is i */
133 tmp
= mid
= i
; /* value of i after swap */
146 * Look at sizes of the two partitions, do the smaller
147 * one first by recursion, then do the larger one by
148 * making sure lo is its size, base and max are update
149 * correctly, and branching back. But only repeat
150 * (recursively or by branching) if the partition is
151 * of at least size THRESH.
153 i
= (j
= mid
) + size
;
154 if ((lo
= j
- base
) <= (hi
= max
- i
))
157 qst(size
, compar
, base
, j
);
164 qst(size
, compar
, i
, max
);
167 } while (lo
>= thresh
);
172 * First, set up some global parameters for qst to share. Then, quicksort
173 * with qst(), and then a cleanup insertion sort ourselves. Sound simple?
179 qsort(void *base0
, size_t n
, size_t size
, int (__cdecl
*compar
)(const void*, const void*))
181 char *base
= (char *)base0
;
182 char c
, *i
, *j
, *lo
, *hi
;
191 thresh
= size
* THRESH
;
192 max
= base
+ n
* size
;
195 qst(size
, compar
, base
, max
);
203 * First put smallest element, which must be in the first THRESH, in
204 * the first position as a sentinel. This is done just by searching
205 * the first THRESH elements (or the first n if n < THRESH), finding
206 * the min, and swapping it into the first position.
208 for (j
= lo
= base
; (lo
+= size
) < hi
; )
209 if (compar(j
, lo
) > 0)
213 /* swap j into place */
214 for (i
= base
, hi
= base
+ size
; i
< hi
; )
222 * With our sentinel in place, we now run the following hyper-fast
223 * insertion sort. For each remaining element, min, from [1] to [n-1],
224 * set hi to the index of the element AFTER which this one goes.
225 * Then, do the standard insertion sort shift on a character at a time
226 * basis for each element in the frob.
228 for (min
= base
; (hi
= min
+= size
) < max
; )
230 while (compar(hi
-= size
, min
) > 0)
232 if ((hi
+= size
) != min
) {
233 for (lo
= min
+ size
; --lo
>= min
; )
236 for (i
= j
= lo
; (j
-= size
) >= hi
; i
= j
)