xref: /openbsd-src/lib/libc/stdlib/qsort.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
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.
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.
23.\"
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
34.\" SUCH DAMAGE.
35.\"
36.\"	$OpenBSD: qsort.3,v 1.6 2000/08/09 15:51:21 aaron Exp $
37.\"
38.Dd June 4, 1993
39.Dt QSORT 3
40.Os
41.Sh NAME
42.Nm qsort ,
43.Nm heapsort ,
44.Nm mergesort
45.Nd sort functions
46.Sh SYNOPSIS
47.Fd #include <stdlib.h>
48.Ft void
49.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
50.Ft int
51.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
52.Ft int
53.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
54.Sh DESCRIPTION
55The
56.Fn qsort
57function is a modified partition-exchange sort, or quicksort.
58The
59.Fn heapsort
60function is a modified selection sort.
61The
62.Fn mergesort
63function is a modified merge sort with exponential search
64intended for sorting data with pre-existing order.
65.Pp
66The
67.Fn qsort
68and
69.Fn heapsort
70functions sort an array of
71.Fa nmemb
72objects, the initial member of which is pointed to by
73.Fa base .
74The size of each object is specified by
75.Fa size .
76.Fn mergesort
77behaves similarly, but
78.Em requires
79that
80.Fa size
81be greater than
82.Dq "sizeof(void *) / 2" .
83.Pp
84The contents of the array
85.Fa base
86are sorted in ascending order according to
87a comparison function pointed to by
88.Fa compar ,
89which requires two arguments pointing to the objects being
90compared.
91.Pp
92The comparison function must return an integer less than, equal to, or
93greater than zero if the first argument is considered to be respectively
94less than, equal to, or greater than the second.
95.Pp
96The functions
97.Fn qsort
98and
99.Fn heapsort
100are
101.Em not
102stable, that is, if two members compare as equal, their order in
103the sorted array is undefined.
104The function
105.Fn mergesort
106is stable.
107.Pp
108The
109.Fn qsort
110function is an implementation of C.A.R. Hoare's
111.Dq quicksort
112algorithm,
113a variant of partition-exchange sorting; in particular, see D.E. Knuth's
114Algorithm Q.
115.Fn qsort
116takes O N lg N average time.
117This implementation uses median selection to avoid its
118O N**2 worst-case behavior.
119.Pp
120The
121.Fn heapsort
122function is an implementation of J.W.J. William's
123.Dq heapsort
124algorithm,
125a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
126.Fn heapsort
127takes O N lg N worst-case time.
128Its
129.Em only
130advantage over
131.Fn qsort
132is that it uses almost no additional memory; while
133.Fn qsort
134does not allocate memory, it is implemented using recursion.
135.Pp
136The function
137.Fn mergesort
138requires additional memory of size
139.Fa nmemb *
140.Fa size
141bytes; it should be used only when space is not at a premium.
142.Fn mergesort
143is optimized for data with pre-existing order; its worst case
144time is O N lg N; its best case is O N.
145.Pp
146Normally,
147.Fn qsort
148is faster than
149.Fn mergesort
150is faster than
151.Fn heapsort .
152Memory availability and pre-existing order in the data can make this untrue.
153.Sh RETURN VALUES
154The
155.Fn qsort
156function returns no value.
157.Pp
158Upon successful completion,
159.Fn heapsort
160and
161.Fn mergesort
162return 0.
163Otherwise, they return \-1 and the global variable
164.Va errno
165is set to indicate the error.
166.Sh ERRORS
167The
168.Fn heapsort
169function succeeds unless:
170.Bl -tag -width Er
171.It Bq Er EINVAL
172The
173.Fa size
174argument is zero, or, the
175.Fa size
176argument to
177.Fn mergesort
178is less than
179.Dq "sizeof(void *) / 2" .
180.It Bq Er ENOMEM
181.Fn heapsort
182or
183.Fn mergesort
184were unable to allocate memory.
185.El
186.Sh COMPATIBILITY
187Previous versions of
188.Fn qsort
189did not permit the comparison routine itself to call
190.Fn qsort 3 .
191This is no longer true.
192.Sh SEE ALSO
193.Xr sort 1 ,
194.Xr radixsort 3
195.Rs
196.%A Hoare, C.A.R.
197.%D 1962
198.%T "Quicksort"
199.%J "The Computer Journal"
200.%V 5:1
201.%P pp. 10-15
202.Re
203.Rs
204.%A Williams, J.W.J
205.%D 1964
206.%T "Heapsort"
207.%J "Communications of the ACM"
208.%V 7:1
209.%P pp. 347-348
210.Re
211.Rs
212.%A Knuth, D.E.
213.%D 1968
214.%B "The Art of Computer Programming"
215.%V Vol. 3
216.%T "Sorting and Searching"
217.%P pp. 114-123, 145-149
218.Re
219.Rs
220.%A Mcilroy, P.M.
221.%T "Optimistic Sorting and Information Theoretic Complexity"
222.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
223.%V January 1992
224.Re
225.Rs
226.%A Bentley, J.L.
227.%T "Engineering a Sort Function"
228.%J "bentley@research.att.com"
229.%V January 1992
230.Re
231.Sh STANDARDS
232The
233.Fn qsort
234function conforms to
235.St -ansiC .
236