1 .\" Copyright (c) 1990, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" This code is derived from software contributed to Berkeley by
5 .\" the American National Standards Committee X3, on Information
6 .\" Processing Systems.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
33 .\" $FreeBSD: src/lib/libc/stdlib/qsort.3,v 1.17 2007/01/09 00:28:10 imp Exp $
35 .Dd September 30, 2003
60 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
68 .Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
76 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
84 .Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
92 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
100 .Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
109 .Fa "int \*[lp]*compar\*[rp]\*[lp]void *, const void *, const void *\*[rp]"
114 function is a modified partition-exchange sort, or quicksort.
117 function is a modified selection sort.
120 function is a modified merge sort with exponential search,
121 intended for sorting data with pre-existing order.
127 functions sort an array of
129 objects, the initial member of which is pointed to by
131 The size of each object is specified by
136 behaves similarly, but
140 be greater than or equal to
141 .Dq "sizeof(void *) / 2" .
143 The contents of the array
145 are sorted in ascending order according to
146 a comparison function pointed to by
148 which requires two arguments pointing to the objects being
151 The comparison function must return an integer less than, equal to, or
152 greater than zero if the first argument is considered to be respectively
153 less than, equal to, or greater than the second.
157 function behaves identically to
159 except that it takes an additional argument,
161 which is passed unchanged as the first argument to function pointed to
163 This allows the comparison function to access additional
164 data without using global variables, and thus
166 is suitable for use in functions which must be reentrant.
168 The algorithms implemented by
175 stable; that is, if two members compare as equal, their order in
176 the sorted array is undefined.
185 functions are an implementation of C.A.R.
189 a variant of partition-exchange sorting; in particular, see
193 takes O N lg N average time.
194 This implementation uses median selection to avoid its
195 O N**2 worst-case behavior.
199 function is an implementation of
200 .An "J.W.J. William" Ns 's
203 a variant of selection sorting; in particular, see
204 .An "D.E. Knuth" Ns 's
207 takes O N lg N worst-case time.
212 is that it uses almost no additional memory; while
214 does not allocate memory, it is implemented using recursion.
218 requires additional memory of size
221 bytes; it should be used only when space is not at a premium.
225 is optimized for data with pre-existing order; its worst case
226 time is O N lg N; its best case is O N.
234 Memory availability and pre-existing order in the data can make this
236 #ifdef UNIFDEF_BLOCKS
243 routines are like the corresponding routines without the _b suffix, expect
246 callback is a block pointer instead of a function pointer.
250 #ifdef UNIFDEF_BLOCKS
261 #ifdef UNIFDEF_BLOCKS
262 .ds HEAPSORT_B heapsort_b
263 .ds MERGESORT_B mergesort_b
265 .Rv -std heapsort \*[HEAPSORT_B] mergesort \*[MERGESORT_B]
269 did not permit the comparison routine itself to call
271 This is no longer true.
274 #ifdef UNIFDEF_BLOCKS
285 functions succeed unless:
290 argument is zero, or,
295 #ifdef UNIFDEF_BLOCKS
300 .Dq "sizeof(void *) / 2" .
303 #ifdef UNIFDEF_BLOCKS
315 were unable to allocate memory.
324 .%J "The Computer Journal"
332 .%J "Communications of the ACM"
339 .%B "The Art of Computer Programming"
341 .%T "Sorting and Searching"
342 .%P pp. 114-123, 145-149
346 .%T "Optimistic Sorting and Information Theoretic Complexity"
347 .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
353 .%T "Engineering a Sort Function"
354 .%J "Software--Practice and Experience"