1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
25 /* Byte-wise swap two items of size SIZE. */
26 #define SWAP(a, b, size) \
29 register size_t __size = (size); \
30 register char *__a = (a), *__b = (b); \
36 } while (--__size > 0); \
39 /* Discontinue quicksort algorithm when partition gets below this size.
40 This particular magic number was chosen to work best on a Sun 4/260. */
43 /* Stack node declarations used to store unfulfilled partition obligations. */
51 /* The next 4 #defines implement a very fast in-line stack abstraction. */
52 #define STACK_SIZE (8 * sizeof(unsigned long int))
53 #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
54 #define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi)))
55 #define STACK_NOT_EMPTY (stack < top)
58 /* Order size using quicksort. This implementation incorporates
59 four optimizations discussed in Sedgewick:
61 1. Non-recursive, using an explicit stack of pointer that store the
62 next array partition to sort. To save time, this maximum amount
63 of space required to store an array of MAX_INT is allocated on the
64 stack. Assuming a 32-bit integer, this needs only 32 *
65 sizeof(stack_node) == 136 bits. Pretty cheap, actually.
67 2. Chose the pivot element using a median-of-three decision tree.
68 This reduces the probability of selecting a bad pivot value and
69 eliminates certain extraneous comparisons.
71 3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
72 insertion sort to order the MAX_THRESH items within each partition.
73 This is a big win, since insertion sort is faster for small, mostly
74 sorted array segements.
76 4. The larger of the two sub-partitions is always pushed onto the
77 stack first, with the algorithm then concentrating on the
78 smaller partition. This *guarantees* no more than log (n)
79 stack size is needed (actually O(1) in this case)! */
82 quicksort (void *pbase, size_t total_elems, size_t size,
83 int (*cmp) (const void *, const void *, void *),
86 register char *base_ptr = (char *) pbase;
88 /* Allocating SIZE bytes for a pivot buffer facilitates a better
89 algorithm below since we can do comparisons directly on the pivot. */
90 char *pivot_buffer = xmalloc (size);
91 const size_t max_thresh = MAX_THRESH * size;
95 /* Avoid lossage with unsigned arithmetic below. */
100 if (total_elems > MAX_THRESH)
103 char *hi = &lo[size * (total_elems - 1)];
104 /* Largest size needed for 32-bit int!!! */
105 stack_node stack[STACK_SIZE];
106 stack_node *top = stack + 1;
108 while (STACK_NOT_EMPTY)
113 char *pivot = pivot_buffer;
115 /* Select median value from among LO, MID, and HI. Rearrange
116 LO and HI so the three values are sorted. This lowers the
117 probability of picking a pathological pivot value and
118 skips a comparison for both the LEFT_PTR and RIGHT_PTR. */
120 char *mid = lo + size * ((hi - lo) / size >> 1);
122 if ((*cmp) ((void *) mid, (void *) lo, param) < 0)
123 SWAP (mid, lo, size);
124 if ((*cmp) ((void *) hi, (void *) mid, param) < 0)
125 SWAP (mid, hi, size);
128 if ((*cmp) ((void *) mid, (void *) lo, param) < 0)
129 SWAP (mid, lo, size);
131 memcpy (pivot, mid, size);
132 pivot = pivot_buffer;
134 left_ptr = lo + size;
135 right_ptr = hi - size;
137 /* Here's the famous ``collapse the walls'' section of quicksort.
138 Gotta like those tight inner loops! They are the main reason
139 that this algorithm runs much faster than others. */
142 while ((*cmp) ((void *) left_ptr, (void *) pivot, param) < 0)
145 while ((*cmp) ((void *) pivot, (void *) right_ptr, param) < 0)
148 if (left_ptr < right_ptr)
150 SWAP (left_ptr, right_ptr, size);
154 else if (left_ptr == right_ptr)
161 while (left_ptr <= right_ptr);
163 /* Set up pointers for next iteration. First determine whether
164 left and right partitions are below the threshold size. If so,
165 ignore one or both. Otherwise, push the larger partition's
166 bounds on the stack and continue sorting the smaller one. */
168 if ((size_t) (right_ptr - lo) <= max_thresh)
170 if ((size_t) (hi - left_ptr) <= max_thresh)
171 /* Ignore both small partitions. */
174 /* Ignore small left partition. */
177 else if ((size_t) (hi - left_ptr) <= max_thresh)
178 /* Ignore small right partition. */
180 else if ((right_ptr - lo) > (hi - left_ptr))
182 /* Push larger left partition indices. */
183 PUSH (lo, right_ptr);
188 /* Push larger right partition indices. */
195 /* Once the BASE_PTR array is partially sorted by quicksort the rest
196 is completely sorted using insertion sort, since this is efficient
197 for partitions below MAX_THRESH size. BASE_PTR points to the beginning
198 of the array to sort, and END_PTR points at the very last element in
199 the array (*not* one beyond it!). */
201 #define min(x, y) ((x) < (y) ? (x) : (y))
204 char *const end_ptr = &base_ptr[size * (total_elems - 1)];
205 char *tmp_ptr = base_ptr;
206 char *thresh = min (end_ptr, base_ptr + max_thresh);
207 register char *run_ptr;
209 /* Find smallest element in first threshold and place it at the
210 array's beginning. This is the smallest array element,
211 and the operation speeds up insertion sort's inner loop. */
213 for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
214 if ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, param) < 0)
217 if (tmp_ptr != base_ptr)
218 SWAP (tmp_ptr, base_ptr, size);
220 /* Insertion sort, running from left-hand-side up to right-hand-side. */
222 run_ptr = base_ptr + size;
223 while ((run_ptr += size) <= end_ptr)
225 tmp_ptr = run_ptr - size;
226 while ((*cmp) ((void *) run_ptr, (void *) tmp_ptr, param) < 0)
230 if (tmp_ptr != run_ptr)
234 trav = run_ptr + size;
235 while (--trav >= run_ptr)
240 for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)