Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
9.4 KiB

4 years ago
  1. /***
  2. *qsort.c - quicksort algorithm; qsort() library function for sorting arrays
  3. *
  4. * Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * To implement the qsort() routine for sorting arrays.
  8. *
  9. *Revision History:
  10. * 06-22-84 RN author
  11. * 03-25-85 RN added pre-check for elements already in order to
  12. * eliminate worst-case behavior.
  13. * 05-18-86 TC changed to recurse on the smallest piece to avoid
  14. * piece. unneccesary stack usage, and to iterate on
  15. * largest
  16. * 01-09-87 BCM fixed huge-array case where (num-1) * wid computation
  17. * was overflowing (large/compact models only)
  18. * 06-13-89 PHG made more efficient, many more comments, removed
  19. * recursion
  20. * 10-30-89 JCR Added _cdecl to prototypes
  21. * 03-15-90 GJF Replaced _cdecl with _CALLTYPE1 and added #include
  22. * <cruntime.h>. Also, fixed the copyright.
  23. * 04-05-90 GJF Made shortsort() and swap() _CALLTYPE4. Also, added
  24. * #include <search.h>.
  25. * 10-04-90 GJF New-style function declarators.
  26. * 12-28-90 SRW Added _CRUISER_ conditional around check_stack pragmas
  27. * 01-24-91 SRW Added missing close comment in swap procedure
  28. * 11-19-91 GJF Do the swap one character at a time to avoid alignment
  29. * woes.
  30. *
  31. *******************************************************************************/
  32. #include <cruntime.h>
  33. #include <stdlib.h>
  34. #include <search.h>
  35. /* prototypes for local routines */
  36. static void _CALLTYPE4 shortsort(char *lo, char *hi, unsigned width,
  37. int (_CALLTYPE1 *comp)(const void *, const void *));
  38. static void _CALLTYPE4 swap(char *p, char *q, unsigned int width);
  39. /* this parameter defines the cutoff between using quick sort and
  40. insertion sort for arrays; arrays with lengths shorter or equal to the
  41. below value use insertion sort */
  42. #define CUTOFF 8 /* testing shows that this is good value */
  43. /***
  44. *qsort(base, num, wid, comp) - quicksort function for sorting arrays
  45. *
  46. *Purpose:
  47. * quicksort the array of elements
  48. * side effects: sorts in place
  49. *
  50. *Entry:
  51. * char *base = pointer to base of array
  52. * unsigned num = number of elements in the array
  53. * unsigned width = width in bytes of each array element
  54. * int (*comp)() = pointer to function returning analog of strcmp for
  55. * strings, but supplied by user for comparing the array elements.
  56. * it accepts 2 pointers to elements and returns neg if 1<2, 0 if
  57. * 1=2, pos if 1>2.
  58. *
  59. *Exit:
  60. * returns void
  61. *
  62. *Exceptions:
  63. *
  64. *******************************************************************************/
  65. #ifdef _CRUISER_
  66. #pragma check_stack(on) /* lots of locals */
  67. #endif /* ndef _CRUISER_ */
  68. /* sort the array between lo and hi (inclusive) */
  69. void _CALLTYPE1 qsort (
  70. void *base,
  71. unsigned num,
  72. unsigned width,
  73. int (_CALLTYPE1 *comp)(const void *, const void *)
  74. )
  75. {
  76. char *lo, *hi; /* ends of sub-array currently sorting */
  77. char *mid; /* points to middle of subarray */
  78. char *loguy, *higuy; /* traveling pointers for partition step */
  79. unsigned size; /* size of the sub-array */
  80. char *lostk[30], *histk[30];
  81. int stkptr; /* stack for saving sub-array to be processed */
  82. /* Note: the number of stack entries required is no more than
  83. 1 + log2(size), so 30 is sufficient for any array */
  84. if (num < 2 || width == 0)
  85. return; /* nothing to do */
  86. stkptr = 0; /* initialize stack */
  87. lo = base;
  88. hi = (char *)base + width * (num-1); /* initialize limits */
  89. /* this entry point is for pseudo-recursion calling: setting
  90. lo and hi and jumping to here is like recursion, but stkptr is
  91. prserved, locals aren't, so we preserve stuff on the stack */
  92. recurse:
  93. size = (hi - lo) / width + 1; /* number of el's to sort */
  94. /* below a certain size, it is faster to use a O(n^2) sorting method */
  95. if (size <= CUTOFF) {
  96. shortsort(lo, hi, width, comp);
  97. }
  98. else {
  99. /* First we pick a partititioning element. The efficiency of the
  100. algorithm demands that we find one that is approximately the
  101. median of the values, but also that we select one fast. Using
  102. the first one produces bad performace if the array is already
  103. sorted, so we use the middle one, which would require a very
  104. wierdly arranged array for worst case performance. Testing shows
  105. that a median-of-three algorithm does not, in general, increase
  106. performance. */
  107. mid = lo + (size / 2) * width; /* find middle element */
  108. swap(mid, lo, width); /* swap it to beginning of array */
  109. /* We now wish to partition the array into three pieces, one
  110. consisiting of elements <= partition element, one of elements
  111. equal to the parition element, and one of element >= to it. This
  112. is done below; comments indicate conditions established at every
  113. step. */
  114. loguy = lo;
  115. higuy = hi + width;
  116. /* Note that higuy decreases and loguy increases on every iteration,
  117. so loop must terminate. */
  118. for (;;) {
  119. /* lo <= loguy < hi, lo < higuy <= hi + 1,
  120. A[i] <= A[lo] for lo <= i <= loguy,
  121. A[i] >= A[lo] for higuy <= i <= hi */
  122. do {
  123. loguy += width;
  124. } while (loguy <= hi && comp(loguy, lo) <= 0);
  125. /* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
  126. either loguy > hi or A[loguy] > A[lo] */
  127. do {
  128. higuy -= width;
  129. } while (higuy > lo && comp(higuy, lo) >= 0);
  130. /* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
  131. either higuy <= lo or A[higuy] < A[lo] */
  132. if (higuy < loguy)
  133. break;
  134. /* if loguy > hi or higuy <= lo, then we would have exited, so
  135. A[loguy] > A[lo], A[higuy] < A[lo],
  136. loguy < hi, highy > lo */
  137. swap(loguy, higuy, width);
  138. /* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
  139. of loop is re-established */
  140. }
  141. /* A[i] >= A[lo] for higuy < i <= hi,
  142. A[i] <= A[lo] for lo <= i < loguy,
  143. higuy < loguy, lo <= higuy <= hi
  144. implying:
  145. A[i] >= A[lo] for loguy <= i <= hi,
  146. A[i] <= A[lo] for lo <= i <= higuy,
  147. A[i] = A[lo] for higuy < i < loguy */
  148. swap(lo, higuy, width); /* put partition element in place */
  149. /* OK, now we have the following:
  150. A[i] >= A[higuy] for loguy <= i <= hi,
  151. A[i] <= A[higuy] for lo <= i < higuy
  152. A[i] = A[lo] for higuy <= i < loguy */
  153. /* We've finished the partition, now we want to sort the subarrays
  154. [lo, higuy-1] and [loguy, hi].
  155. We do the smaller one first to minimize stack usage.
  156. We only sort arrays of length 2 or more.*/
  157. if ( higuy - 1 - lo >= hi - loguy ) {
  158. if (lo + width < higuy) {
  159. lostk[stkptr] = lo;
  160. histk[stkptr] = higuy - width;
  161. ++stkptr;
  162. } /* save big recursion for later */
  163. if (loguy < hi) {
  164. lo = loguy;
  165. goto recurse; /* do small recursion */
  166. }
  167. }
  168. else {
  169. if (loguy < hi) {
  170. lostk[stkptr] = loguy;
  171. histk[stkptr] = hi;
  172. ++stkptr; /* save big recursion for later */
  173. }
  174. if (lo + width < higuy) {
  175. hi = higuy - width;
  176. goto recurse; /* do small recursion */
  177. }
  178. }
  179. }
  180. /* We have sorted the array, except for any pending sorts on the stack.
  181. Check if there are any, and do them. */
  182. --stkptr;
  183. if (stkptr >= 0) {
  184. lo = lostk[stkptr];
  185. hi = histk[stkptr];
  186. goto recurse; /* pop subarray from stack */
  187. }
  188. else
  189. return; /* all subarrays done */
  190. }
  191. #ifdef _CRUISER_
  192. #pragma check_stack() /* revert to command line behaviour */
  193. #endif /* ndef _CRUISER_ */
  194. /***
  195. *shortsort(hi, lo, width, comp) - insertion sort for sorting short arrays
  196. *
  197. *Purpose:
  198. * sorts the sub-array of elements between lo and hi (inclusive)
  199. * side effects: sorts in place
  200. * assumes that lo < hi
  201. *
  202. *Entry:
  203. * char *lo = pointer to low element to sort
  204. * char *hi = pointer to high element to sort
  205. * unsigned width = width in bytes of each array element
  206. * int (*comp)() = pointer to function returning analog of strcmp for
  207. * strings, but supplied by user for comparing the array elements.
  208. * it accepts 2 pointers to elements and returns neg if 1<2, 0 if
  209. * 1=2, pos if 1>2.
  210. *
  211. *Exit:
  212. * returns void
  213. *
  214. *Exceptions:
  215. *
  216. *******************************************************************************/
  217. static void _CALLTYPE4 shortsort (
  218. char *lo,
  219. char *hi,
  220. unsigned width,
  221. int (_CALLTYPE1 *comp)(const void *, const void *)
  222. )
  223. {
  224. char *p, *max;
  225. /* Note: in assertions below, i and j are alway inside original bound of
  226. array to sort. */
  227. while (hi > lo) {
  228. /* A[i] <= A[j] for i <= j, j > hi */
  229. max = lo;
  230. for (p = lo+width; p <= hi; p += width) {
  231. /* A[i] <= A[max] for lo <= i < p */
  232. if (comp(p, max) > 0) {
  233. max = p;
  234. }
  235. /* A[i] <= A[max] for lo <= i <= p */
  236. }
  237. /* A[i] <= A[max] for lo <= i <= hi */
  238. swap(max, hi, width);
  239. /* A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi */
  240. hi -= width;
  241. /* A[i] <= A[j] for i <= j, j > hi, loop top condition established */
  242. }
  243. /* A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
  244. so array is sorted */
  245. }
  246. /***
  247. *swap(a, b, width) - swap two elements
  248. *
  249. *Purpose:
  250. * swaps the two array elements of size width
  251. *
  252. *Entry:
  253. * char *a, *b = pointer to two elements to swap
  254. * unsigned width = width in bytes of each array element
  255. *
  256. *Exit:
  257. * returns void
  258. *
  259. *Exceptions:
  260. *
  261. *******************************************************************************/
  262. static void _CALLTYPE4 swap (
  263. char *a,
  264. char *b,
  265. unsigned width
  266. )
  267. {
  268. char tmp;
  269. if ( a != b )
  270. /* Do the swap one character at a time to avoid potential alignment
  271. problems. */
  272. while ( width-- ) {
  273. tmp = *a;
  274. *a++ = *b;
  275. *b++ = tmp;
  276. }
  277. }