1e1dc8c712d717e318998b06f40e441f6ccdbab8

1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */

3 #include <stdlib.h>

4 #include <search.h>

6 /*-

7 * Copyright (c) 1980, 1983 The Regents of the University of California.

8 * All rights reserved.

9 *

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.

23 */

25 /*

26 * qsort.c:

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.

32 */

37 /*

38 * qst:

39 * Do a quicksort

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).

50 */

52 static void

54 {

65 /*

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.

73 */

78 {

81 {

82 /* switch to first loser */

86 }

88 {

95 }

96 }

97 /*

98 * Semi-standard quicksort partitioning/swapping

99 */

101 {

105 {

107 {

110 }

113 {

114 /* j <-> mid, new mid is j */

116 }

117 else

118 {

119 /* i <-> j */

122 }

124 }

126 {

128 }

129 else

130 {

131 /* i <-> mid, new mid is i */

135 }

136 swap:

144 }

145 /*

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.

152 */

155 {

160 }

161 else

162 {

166 }

168 }

170 /*

171 * qsort:

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?

174 * It's not...

175 *

176 * @implemented

177 */

178 void

180 {

194 {

197 }

198 else

199 {

201 }

202 /*

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.

207 */

212 {

213 /* swap j into place */

215 {

219 }

220 }

221 /*

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.

227 */

229 {

234 {

239 }

240 }

241 }

242 }