1 .\" $NetBSD: qsort.3,v 1.12 2003/05/10 12:24:54 wiz Exp $
3 .\" Copyright (c) 1990, 1991, 1993
4 .\" The Regents of the University of California. All rights reserved.
6 .\" This code is derived from software contributed to Berkeley by
7 .\" the American National Standards Committee X3, on Information
8 .\" Processing Systems.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
13 .\" 1. Redistributions of source code must retain the above copyright
14 .\" notice, this list of conditions and the following disclaimer.
15 .\" 2. Redistributions in binary form must reproduce the above copyright
16 .\" notice, this list of conditions and the following disclaimer in the
17 .\" documentation and/or other materials provided with the distribution.
18 .\" 3. Neither the name of the University nor the names of its contributors
19 .\" may be used to endorse or promote products derived from this software
20 .\" without specific prior written permission.
22 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93
49 .Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
51 .Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
53 .Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
57 function is a modified partition-exchange sort, or quicksort.
60 function is a modified selection sort.
63 function is a modified merge sort with exponential search
64 intended for sorting data with pre-existing order.
70 functions sort an array of
72 objects, the initial member of which is pointed to by
74 The size of each object is specified by
77 behaves similarly, but
82 .Dq "sizeof(void *) / 2" .
84 The contents of the array
86 are sorted in ascending order according to
87 a comparison function pointed to by
89 which requires two arguments pointing to the objects being
92 The comparison function must return an integer less than, equal to, or
93 greater than zero if the first argument is considered to be respectively
94 less than, equal to, or greater than the second.
102 stable, that is, if two members compare as equal, their order in
103 the sorted array is undefined.
110 function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
111 a variant of partition-exchange sorting; in particular, see D.E. Knuth's
114 takes O N lg N average time.
115 This implementation uses median selection to avoid its
116 O N**2 worst-case behavior.
120 function is an implementation of J.W.J. William's ``heapsort'' algorithm,
121 a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
123 takes O N lg N worst-case time.
128 is that it uses almost no additional memory; while
130 does not allocate memory, it is implemented using recursion.
134 requires additional memory of size
137 bytes; it should be used only when space is not at a premium.
139 is optimized for data with pre-existing order; its worst case
140 time is O N lg N; its best case is O N.
148 Memory availability and pre-existing order in the data can make this
156 Upon successful completion,
161 Otherwise, they return \-1 and the global variable
163 is set to indicate the error.
167 function succeeds 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
221 .%T "Optimistic Sorting and Information Theoretic Complexity"
222 .%J "Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
226 .%A Bentley, J.L. and McIlroy, M.D.
228 .%T "Engineering a Sort Function"
229 .%J "Software-Practice and Experience"