1 /* Copyright (C) 1991 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 modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 1, or (at your option)
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
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with the GNU C Library; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* Byte-wise swap two items of size SIZE. */
24 #define SWAP(a, b, size) \
27 register size_t __size = (size); \
28 register char *__a = (a), *__b = (b); \
34 } while (--__size > 0); \
37 /* Discontinue quicksort algorithm when partition gets below this size.
38 This particular magic number was chosen to work best on a Sun 4/260. */
41 /* Stack node declarations used to store unfulfilled partition obligations. */
48 /* The next 4 #defines implement a very fast in-line stack abstraction. */
49 #define STACK_SIZE (8 * sizeof(unsigned long int))
50 #define PUSH(low, high) ((void) ((top->lo = (low)), (top->hi = (high)), ++top))
51 #define POP(low, high) ((void) (--top, (low = top->lo), (high = top->hi)))
52 #define STACK_NOT_EMPTY (stack < top)
55 /* Order size using quicksort. This implementation incorporates
56 four optimizations discussed in Sedgewick:
58 1. Non-recursive, using an explicit stack of pointer that store the
59 next array partition to sort. To save time, this maximum amount
60 of space required to store an array of MAX_INT is allocated on the
61 stack. Assuming a 32-bit integer, this needs only 32 *
62 sizeof(stack_node) == 136 bits. Pretty cheap, actually.
64 2. Chose the pivot element using a median-of-three decision tree.
65 This reduces the probability of selecting a bad pivot value and
66 eliminates certain extraneous comparisons.
68 3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
69 insertion sort to order the MAX_THRESH items within each partition.
70 This is a big win, since insertion sort is faster for small, mostly
71 sorted array segements.
73 4. The larger of the two sub-partitions is always pushed onto the
74 stack first, with the algorithm then concentrating on the
75 smaller partition. This *guarantees* no more than log (n)
76 stack size is needed (actually O(1) in this case)! */
79 DEFUN(qsort, (pbase, total_elems, size, cmp),
80 PTR CONST pbase AND size_t total_elems AND size_t size AND
81 int EXFUN((*cmp), (CONST PTR, CONST PTR)))
83 register char *base_ptr = (char *) pbase;
85 /* Allocating SIZE bytes for a pivot buffer facilitates a better
86 algorithm below since we can do comparisons directly on the pivot. */
87 char *pivot_buffer = (char *) __alloca (size);
88 CONST size_t max_thresh = MAX_THRESH * size;
90 if (total_elems > MAX_THRESH)
93 char *hi = &lo[size * (total_elems - 1)];
94 /* Largest size needed for 32-bit int!!! */
95 stack_node stack[STACK_SIZE];
96 stack_node *top = stack + 1;
98 while (STACK_NOT_EMPTY)
103 char *pivot = pivot_buffer;
105 /* Select median value from among LO, MID, and HI. Rearrange
106 LO and HI so the three values are sorted. This lowers the
107 probability of picking a pathological pivot value and
108 skips a comparison for both the LEFT_PTR and RIGHT_PTR. */
110 char *mid = lo + size * ((hi - lo) / size >> 1);
112 if ((*cmp)((PTR) mid, (PTR) lo) < 0)
114 if ((*cmp)((PTR) hi, (PTR) mid) < 0)
118 if ((*cmp)((PTR) mid, (PTR) lo) < 0)
121 memcpy(pivot, mid, size);
122 pivot = pivot_buffer;
124 left_ptr = lo + size;
125 right_ptr = hi - size;
127 /* Here's the famous ``collapse the walls'' section of quicksort.
128 Gotta like those tight inner loops! They are the main reason
129 that this algorithm runs much faster than others. */
132 while ((*cmp)((PTR) left_ptr, (PTR) pivot) < 0)
135 while ((*cmp)((PTR) pivot, (PTR) right_ptr) < 0)
138 if (left_ptr < right_ptr)
140 SWAP(left_ptr, right_ptr, size);
144 else if (left_ptr == right_ptr)
151 while (left_ptr <= right_ptr);
153 /* Set up pointers for next iteration. First determine whether
154 left and right partitions are below the threshold size. If so,
155 ignore one or both. Otherwise, push the larger partition's
156 bounds on the stack and continue sorting the smaller one. */
158 if ((size_t) (right_ptr - lo) <= max_thresh)
160 if ((size_t) (hi - left_ptr) <= max_thresh)
161 /* Ignore both small partitions. */
164 /* Ignore small left partition. */
167 else if ((size_t) (hi - left_ptr) <= max_thresh)
168 /* Ignore small right partition. */
170 else if ((right_ptr - lo) > (hi - left_ptr))
172 /* Push larger left partition indices. */
178 /* Push larger right partition indices. */
185 /* Once the BASE_PTR array is partially sorted by quicksort the rest
186 is completely sorted using insertion sort, since this is efficient
187 for partitions below MAX_THRESH size. BASE_PTR points to the beginning
188 of the array to sort, and END_PTR points at the very last element in
189 the array (*not* one beyond it!). */
191 #define min(x, y) ((x) < (y) ? (x) : (y))
194 char *CONST end_ptr = &base_ptr[size * (total_elems - 1)];
195 char *tmp_ptr = base_ptr;
196 char *thresh = min(end_ptr, base_ptr + max_thresh);
197 register char *run_ptr;
199 /* Find smallest element in first threshold and place it at the
200 array's beginning. This is the smallest array element,
201 and the operation speeds up insertion sort's inner loop. */
203 for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
204 if ((*cmp)((PTR) run_ptr, (PTR) tmp_ptr) < 0)
207 if (tmp_ptr != base_ptr)
208 SWAP(tmp_ptr, base_ptr, size);
210 /* Insertion sort, running from left-hand-side up to right-hand-side. */
212 run_ptr = base_ptr + size;
213 while ((run_ptr += size) <= end_ptr)
215 tmp_ptr = run_ptr - size;
216 while ((*cmp)((PTR) run_ptr, (PTR) tmp_ptr) < 0)
220 if (tmp_ptr != run_ptr)
224 trav = run_ptr + size;
225 while (--trav >= run_ptr)
230 for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)