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 .\" 3. All advertising materials mentioning features or use of this software
17 .\" must display the following acknowledgement:
18 .\" This product includes software developed by the University of
19 .\" California, Berkeley and its contributors.
20 .\" 4. Neither the name of the University nor the names of its contributors
21 .\" may be used to endorse or promote products derived from this software
22 .\" without specific prior written permission.
24 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
42 .Nm qsort, heapsort, mergesort
45 .Fd #include <stdlib.h>
47 .Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
49 .Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
51 .Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
55 function is a modified partition-exchange sort, or quicksort.
58 function is a modified selection sort.
61 function is a modified merge sort with exponential search
62 intended for sorting data with pre-existing order.
68 functions sort an array of
70 objects, the initial member of which is pointed to by
72 The size of each object is specified by
75 behaves similarly, but
80 .Dq "sizeof(void *) / 2" .
82 The contents of the array
84 are sorted in ascending order according to
85 a comparison function pointed to by
87 which requires two arguments pointing to the objects being
90 The comparison function must return an integer less than, equal to, or
91 greater than zero if the first argument is considered to be respectively
92 less than, equal to, or greater than the second.
100 stable, that is, if two members compare as equal, their order in
101 the sorted array is undefined.
108 function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
109 a variant of partition-exchange sorting; in particular, see D.E. Knuth's
112 takes O N lg N average time.
113 This implementation uses median selection to avoid its
114 O N**2 worst-case behavior.
118 function is an implementation of J.W.J. William's ``heapsort'' algorithm,
119 a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
121 takes O N lg N worst-case time.
126 is that it uses almost no additional memory; while
128 does not allocate memory, it is implemented using recursion.
132 requires additional memory of size
135 bytes; it should be used only when space is not at a premium.
137 is optimized for data with pre-existing order; its worst case
138 time is O N lg N; its best case is O N.
146 Memory availability and pre-existing order in the data can make this
154 Upon successful completion,
159 Otherwise, they return \-1 and the global variable
161 is set to indicate the error.
167 functions succeed unless:
172 argument is zero, or,
178 .Dq "sizeof(void *) / 2" .
183 were unable to allocate memory.
188 did not permit the comparison routine itself to call
190 This is no longer true.
198 .%J "The Computer Journal"
206 .%J "Communications of the ACM"
213 .%B "The Art of Computer Programming"
215 .%T "Sorting and Searching"
216 .%P pp. 114-123, 145-149
220 .%T "Optimistic Sorting and Information Theoretic Complexity"
221 .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
226 .%T "Engineering a Sort Function"
227 .%J "bentley@research.att.com"