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. 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
56 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
63 .Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
70 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]"
78 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
85 .Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
92 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
99 .Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
101 .Fd #define __STDC_WANT_LIB_EXT1__ 1
107 .Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]"
113 function is a modified partition-exchange sort, or quicksort.
116 function is a modified selection sort.
119 function is a modified merge sort with exponential search
120 intended for sorting data with pre-existing order.
126 functions sort an array of
128 objects, the initial member of which is pointed to by
130 The size of each object is specified by
135 behaves similarly, but
140 .Dq "sizeof(void *) / 2" .
142 The contents of the array
144 are sorted in ascending order according to
145 a comparison function pointed to by
147 which requires two arguments pointing to the objects being
150 The comparison function must return an integer less than, equal to, or
151 greater than zero if the first argument is considered to be respectively
152 less than, equal to, or greater than the second.
156 function behaves identically to
158 except that it takes an additional argument,
160 which is passed unchanged as the last argument to function pointed to
162 This allows the comparison function to access additional
163 data without using global variables, and thus
165 is suitable for use in functions which must be reentrant.
168 function behaves identically to
170 except that it takes a block, rather than a function pointer.
172 The algorithms implemented by
179 stable, that is, if two members compare as equal, their order in
180 the sorted array is undefined.
183 function behaves identically to
185 except that it takes a block, rather than a function pointer.
191 function behaves identically to
193 except that it takes a block, rather than a function pointer.
199 functions are an implementation of C.A.R.
203 a variant of partition-exchange sorting; in particular, see
207 takes O N lg N average time.
208 This implementation uses median selection to avoid its
209 O N**2 worst-case behavior.
213 function is an implementation of
214 .An "J.W.J. William" Ns 's
217 a variant of selection sorting; in particular, see
218 .An "D.E. Knuth" Ns 's
221 takes O N lg N worst-case time.
226 is that it uses almost no additional memory; while
228 does not allocate memory, it is implemented using recursion.
232 requires additional memory of size
235 bytes; it should be used only when space is not at a premium.
239 is optimized for data with pre-existing order; its worst case
240 time is O N lg N; its best case is O N.
248 Memory availability and pre-existing order in the data can make this
253 function behaves the same as
254 .Fn qsort_r , except that:
257 The order of arguments is different
259 The order of arguments to
277 is zero, then the runtime-constraint handler is called, and
280 Note that the handler is called before
282 returns the error, and the handler function might not return.
293 function returns zero on success, non-zero on error.
295 .Rv -std heapsort mergesort
297 A sample program that sorts an array of
299 values in place using
301 and then prints the sorted array to standard output is:
307 * Custom comparison function that compares 'int' values through pointers
308 * passed by qsort(3).
311 int_compare(const void *p1, const void *p2)
313 int left = *(const int *)p1;
314 int right = *(const int *)p2;
316 return ((left > right) - (left < right));
320 * Sort an array of 'int' values and print it to standard output.
325 int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 };
326 size_t array_size = sizeof(int_array) / sizeof(int_array[0]);
329 qsort(&int_array, array_size, sizeof(int_array[0]), int_compare);
330 for (k = 0; k < array_size; k++)
331 printf(" %d", int_array[k]);
333 return (EXIT_SUCCESS);
337 The order of arguments for the comparison function used with
339 is different from the one used by
341 and the GNU libc implementation of
343 When porting software written for GNU libc, it is usually possible
348 to work around this problem.
355 and may not be portable to other standards-conforming platforms.
359 did not permit the comparison routine itself to call
361 This is no longer true.
367 functions succeed unless:
372 argument is zero, or,
378 .Dq "sizeof(void *) / 2" .
385 were unable to allocate memory.
394 .%J "The Computer Journal"
402 .%J "Communications of the ACM"
409 .%B "The Art of Computer Programming"
411 .%T "Sorting and Searching"
412 .%P pp. 114-123, 145-149
416 .%T "Optimistic Sorting and Information Theoretic Complexity"
417 .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
423 .%T "Engineering a Sort Function"
424 .%J "Software--Practice and Experience"
440 The variants of these functions that take blocks as arguments first appeared in
442 This implementation was created by David Chisnall.
448 was updated to match POSIX.