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
8.7 KiB

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