Leaked source code of windows server 2003
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.

298 lines
9.4 KiB

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