]> git.saurik.com Git - apple/libc.git/blame - stdlib/FreeBSD/qsort.3
Libc-825.40.1.tar.gz
[apple/libc.git] / stdlib / FreeBSD / qsort.3
CommitLineData
5b2abdfb
A
1.\" Copyright (c) 1990, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
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.
5b2abdfb
A
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.
19.\"
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
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
1f2f436a 33.\" $FreeBSD: src/lib/libc/stdlib/qsort.3,v 1.17 2007/01/09 00:28:10 imp Exp $
5b2abdfb 34.\"
3d9156a7 35.Dd September 30, 2003
5b2abdfb
A
36.Dt QSORT 3
37.Os
38.Sh NAME
ad3c9f2a
A
39.Nm heapsort ,
40#ifdef UNIFDEF_BLOCKS
41.Nm heapsort_b ,
42#endif
43.Nm mergesort ,
44#ifdef UNIFDEF_BLOCKS
45.Nm mergesort_b ,
46#endif
47.Nm qsort ,
48#ifdef UNIFDEF_BLOCKS
49.Nm qsort_b ,
50#endif
51.Nm qsort_r
5b2abdfb 52.Nd sort functions
5b2abdfb
A
53.Sh SYNOPSIS
54.In stdlib.h
ad3c9f2a
A
55.Ft int
56.Fo heapsort
9385eb3d 57.Fa "void *base"
ad3c9f2a
A
58.Fa "size_t nel"
59.Fa "size_t width"
9385eb3d
A
60.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
61.Fc
ad3c9f2a
A
62#ifdef UNIFDEF_BLOCKS
63.Ft int
64.Fo heapsort_b
9385eb3d 65.Fa "void *base"
ad3c9f2a
A
66.Fa "size_t nel"
67.Fa "size_t width"
68.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
9385eb3d 69.Fc
ad3c9f2a 70#endif
5b2abdfb 71.Ft int
ad3c9f2a 72.Fo mergesort
9385eb3d 73.Fa "void *base"
ad3c9f2a
A
74.Fa "size_t nel"
75.Fa "size_t width"
9385eb3d
A
76.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
77.Fc
ad3c9f2a 78#ifdef UNIFDEF_BLOCKS
5b2abdfb 79.Ft int
ad3c9f2a
A
80.Fo mergesort_b
81.Fa "void *base"
82.Fa "size_t nel"
83.Fa "size_t width"
84.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
85.Fc
86#endif
87.Ft void
88.Fo qsort
9385eb3d 89.Fa "void *base"
ad3c9f2a
A
90.Fa "size_t nel"
91.Fa "size_t width"
9385eb3d
A
92.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
93.Fc
ad3c9f2a
A
94#ifdef UNIFDEF_BLOCKS
95.Ft void
96.Fo qsort_b
97.Fa "void *base"
98.Fa "size_t nel"
99.Fa "size_t width"
100.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
101.Fc
102#endif
103.Ft void
104.Fo qsort_r
105.Fa "void *base"
106.Fa "size_t nel"
107.Fa "size_t width"
108.Fa "void *thunk"
109.Fa "int \*[lp]*compar\*[rp]\*[lp]void *, const void *, const void *\*[rp]"
110.Fc
5b2abdfb
A
111.Sh DESCRIPTION
112The
113.Fn qsort
114function is a modified partition-exchange sort, or quicksort.
115The
116.Fn heapsort
117function is a modified selection sort.
118The
119.Fn mergesort
ad3c9f2a 120function is a modified merge sort with exponential search,
5b2abdfb
A
121intended for sorting data with pre-existing order.
122.Pp
123The
124.Fn qsort
125and
126.Fn heapsort
127functions sort an array of
ad3c9f2a 128.Fa nel
5b2abdfb
A
129objects, the initial member of which is pointed to by
130.Fa base .
131The size of each object is specified by
ad3c9f2a 132.Fa width .
9385eb3d
A
133The
134.Fn mergesort
135function
5b2abdfb
A
136behaves similarly, but
137.Em requires
138that
ad3c9f2a
A
139.Fa width
140be greater than or equal to
5b2abdfb
A
141.Dq "sizeof(void *) / 2" .
142.Pp
143The contents of the array
144.Fa base
145are sorted in ascending order according to
146a comparison function pointed to by
147.Fa compar ,
148which requires two arguments pointing to the objects being
149compared.
150.Pp
151The comparison function must return an integer less than, equal to, or
152greater than zero if the first argument is considered to be respectively
153less than, equal to, or greater than the second.
154.Pp
9385eb3d
A
155The
156.Fn qsort_r
157function behaves identically to
158.Fn qsort ,
159except that it takes an additional argument,
160.Fa thunk ,
161which is passed unchanged as the first argument to function pointed to
162.Fa compar .
163This allows the comparison function to access additional
164data without using global variables, and thus
165.Fn qsort_r
166is suitable for use in functions which must be reentrant.
167.Pp
168The algorithms implemented by
169.Fn qsort ,
170.Fn qsort_r ,
5b2abdfb
A
171and
172.Fn heapsort
173are
174.Em not
ad3c9f2a 175stable; that is, if two members compare as equal, their order in
5b2abdfb 176the sorted array is undefined.
9385eb3d 177The
5b2abdfb 178.Fn mergesort
9385eb3d 179algorithm is stable.
5b2abdfb
A
180.Pp
181The
182.Fn qsort
9385eb3d
A
183and
184.Fn qsort_r
3d9156a7
A
185functions are an implementation of C.A.R.
186Hoare's
9385eb3d
A
187.Dq quicksort
188algorithm,
3d9156a7
A
189a variant of partition-exchange sorting; in particular, see
190.An D.E. Knuth Ns 's
191.%T "Algorithm Q" .
9385eb3d 192.Sy Quicksort
5b2abdfb
A
193takes O N lg N average time.
194This implementation uses median selection to avoid its
195O N**2 worst-case behavior.
196.Pp
197The
198.Fn heapsort
3d9156a7
A
199function is an implementation of
200.An "J.W.J. William" Ns 's
9385eb3d
A
201.Dq heapsort
202algorithm,
3d9156a7
A
203a variant of selection sorting; in particular, see
204.An "D.E. Knuth" Ns 's
205.%T "Algorithm H" .
9385eb3d 206.Sy Heapsort
5b2abdfb
A
207takes O N lg N worst-case time.
208Its
209.Em only
210advantage over
211.Fn qsort
212is that it uses almost no additional memory; while
213.Fn qsort
214does not allocate memory, it is implemented using recursion.
215.Pp
216The function
217.Fn mergesort
218requires additional memory of size
ad3c9f2a
A
219.Fa nel *
220.Fa width
5b2abdfb 221bytes; it should be used only when space is not at a premium.
9385eb3d
A
222The
223.Fn mergesort
224function
5b2abdfb
A
225is optimized for data with pre-existing order; its worst case
226time is O N lg N; its best case is O N.
227.Pp
228Normally,
229.Fn qsort
230is faster than
231.Fn mergesort
ad3c9f2a 232which is faster than
5b2abdfb
A
233.Fn heapsort .
234Memory availability and pre-existing order in the data can make this
235untrue.
ad3c9f2a
A
236#ifdef UNIFDEF_BLOCKS
237.Pp
238The
239.Fn heapsort_b ,
240.Fn mergesort_b ,
241and
242.Fn qsort_b
243routines are like the corresponding routines without the _b suffix, expect
244that the
245.Fa compar
246callback is a block pointer instead of a function pointer.
247#endif
5b2abdfb
A
248.Sh RETURN VALUES
249The
ad3c9f2a
A
250#ifdef UNIFDEF_BLOCKS
251.Fn qsort ,
252.Fn qsort_b
253#else
5b2abdfb 254.Fn qsort
ad3c9f2a 255#endif
9385eb3d
A
256and
257.Fn qsort_r
258functions
259return no value.
5b2abdfb 260.Pp
ad3c9f2a
A
261#ifdef UNIFDEF_BLOCKS
262.ds HEAPSORT_B heapsort_b
263.ds MERGESORT_B mergesort_b
264#endif
265.Rv -std heapsort \*[HEAPSORT_B] mergesort \*[MERGESORT_B]
1f2f436a
A
266.Sh COMPATIBILITY
267Previous versions of
268.Fn qsort
269did not permit the comparison routine itself to call
270.Fn qsort 3 .
271This is no longer true.
5b2abdfb
A
272.Sh ERRORS
273The
ad3c9f2a
A
274#ifdef UNIFDEF_BLOCKS
275.Fn heapsort ,
276.Fn heapsort_b ,
277.Fn mergesort ,
278and
279.Fn mergesort_b
280#else
5b2abdfb
A
281.Fn heapsort
282and
283.Fn mergesort
ad3c9f2a 284#endif
5b2abdfb
A
285functions succeed unless:
286.Bl -tag -width Er
287.It Bq Er EINVAL
288The
ad3c9f2a 289.Fa width
5b2abdfb
A
290argument is zero, or,
291the
ad3c9f2a 292.Fa width
5b2abdfb
A
293argument to
294.Fn mergesort
ad3c9f2a
A
295#ifdef UNIFDEF_BLOCKS
296or
297.Fn mergesort_b
298#endif
5b2abdfb
A
299is less than
300.Dq "sizeof(void *) / 2" .
301.It Bq Er ENOMEM
9385eb3d 302The
ad3c9f2a
A
303#ifdef UNIFDEF_BLOCKS
304.Fn heapsort ,
305.Fn heapsort_b ,
306.Fn mergesort ,
307or
308.Fn mergesort_b
309#else
9385eb3d 310.Fn heapsort
5b2abdfb
A
311or
312.Fn mergesort
ad3c9f2a 313#endif
9385eb3d 314functions
5b2abdfb
A
315were unable to allocate memory.
316.El
5b2abdfb
A
317.Sh SEE ALSO
318.Xr sort 1 ,
319.Xr radixsort 3
320.Rs
321.%A Hoare, C.A.R.
322.%D 1962
323.%T "Quicksort"
324.%J "The Computer Journal"
325.%V 5:1
326.%P pp. 10-15
327.Re
328.Rs
329.%A Williams, J.W.J
330.%D 1964
331.%T "Heapsort"
332.%J "Communications of the ACM"
333.%V 7:1
334.%P pp. 347-348
335.Re
336.Rs
337.%A Knuth, D.E.
338.%D 1968
339.%B "The Art of Computer Programming"
340.%V Vol. 3
341.%T "Sorting and Searching"
342.%P pp. 114-123, 145-149
343.Re
344.Rs
3d9156a7 345.%A McIlroy, P.M.
5b2abdfb
A
346.%T "Optimistic Sorting and Information Theoretic Complexity"
347.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
348.%V January 1992
349.Re
350.Rs
351.%A Bentley, J.L.
3d9156a7 352.%A McIlroy, M.D.
5b2abdfb 353.%T "Engineering a Sort Function"
3d9156a7
A
354.%J "Software--Practice and Experience"
355.%V Vol. 23(11)
356.%P pp. 1249-1265
357.%D November\ 1993
5b2abdfb
A
358.Re
359.Sh STANDARDS
360The
361.Fn qsort
362function
363conforms to
364.St -isoC .