xref: /netbsd-src/lib/libc/stdlib/qsort.3 (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1.\"	$NetBSD: qsort.3,v 1.5 1997/11/14 02:04:55 mrg Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" the American National Standards Committee X3, on Information
8.\" Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
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. All advertising materials mentioning features or use of this software
19.\"    must display the following acknowledgement:
20.\"	This product includes software developed by the University of
21.\"	California, Berkeley and its contributors.
22.\" 4. Neither the name of the University nor the names of its contributors
23.\"    may be used to endorse or promote products derived from this software
24.\"    without specific prior written permission.
25.\"
26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36.\" SUCH DAMAGE.
37.\"
38.\"     from: @(#)qsort.3	8.1 (Berkeley) 6/4/93
39.\"
40.Dd June 4, 1993
41.Dt QSORT 3
42.Os
43.Sh NAME
44.Nm qsort ,
45.Nm heapsort ,
46.Nm mergesort
47.Nd sort functions
48.Sh SYNOPSIS
49.Fd #include <stdlib.h>
50.Ft void
51.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
52.Ft int
53.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
54.Ft int
55.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
56.Sh DESCRIPTION
57The
58.Fn qsort
59function is a modified partition-exchange sort, or quicksort.
60The
61.Fn heapsort
62function is a modified selection sort.
63The
64.Fn mergesort
65function is a modified merge sort with exponential search
66intended for sorting data with pre-existing order.
67.Pp
68The
69.Fn qsort
70and
71.Fn heapsort
72functions sort an array of
73.Fa nmemb
74objects, the initial member of which is pointed to by
75.Fa base .
76The size of each object is specified by
77.Fa size .
78.Fn Mergesort
79behaves similarly, but
80.Em requires
81that
82.Fa size
83be greater than
84.Dq "sizeof(void *) / 2" .
85.Pp
86The contents of the array
87.Fa base
88are sorted in ascending order according to
89a comparison function pointed to by
90.Fa compar ,
91which requires two arguments pointing to the objects being
92compared.
93.Pp
94The comparison function must return an integer less than, equal to, or
95greater than zero if the first argument is considered to be respectively
96less than, equal to, or greater than the second.
97.Pp
98The functions
99.Fn qsort
100and
101.Fn heapsort
102are
103.Em not
104stable, that is, if two members compare as equal, their order in
105the sorted array is undefined.
106The function
107.Fn mergesort
108is stable.
109.Pp
110The
111.Fn qsort
112function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
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 ``heapsort'' algorithm,
123a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
124.Fn Heapsort
125takes O N lg N worst-case time.
126Its
127.Em only
128advantage over
129.Fn qsort
130is that it uses almost no additional memory; while
131.Fn qsort
132does not allocate memory, it is implemented using recursion.
133.Pp
134The function
135.Fn mergesort
136requires additional memory of size
137.Fa nmemb *
138.Fa size
139bytes; it should be used only when space is not at a premium.
140.Fn Mergesort
141is optimized for data with pre-existing order; its worst case
142time is O N lg N; its best case is O N.
143.Pp
144Normally,
145.Fn qsort
146is faster than
147.Fn mergesort
148is faster than
149.Fn heapsort .
150Memory availability and pre-existing order in the data can make this
151untrue.
152.Sh RETURN VALUES
153The
154.Fn qsort
155function
156returns 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,
175the
176.Fa size
177argument to
178.Fn mergesort
179is less than
180.Dq "sizeof(void *) / 2" .
181.It Bq Er ENOMEM
182.Fn Heapsort
183or
184.Fn mergesort
185were unable to allocate memory.
186.El
187.Sh COMPATIBILITY
188Previous versions of
189.Fn qsort
190did not permit the comparison routine itself to call
191.Fn qsort 3 .
192This is no longer true.
193.Sh SEE ALSO
194.Xr sort 1 ,
195.Xr radixsort 3
196.Rs
197.%A Hoare, C.A.R.
198.%D 1962
199.%T "Quicksort"
200.%J "The Computer Journal"
201.%V 5:1
202.%P pp. 10-15
203.Re
204.Rs
205.%A Williams, J.W.J
206.%D 1964
207.%T "Heapsort"
208.%J "Communications of the ACM"
209.%V 7:1
210.%P pp. 347-348
211.Re
212.Rs
213.%A Knuth, D.E.
214.%D 1968
215.%B "The Art of Computer Programming"
216.%V Vol. 3
217.%T "Sorting and Searching"
218.%P pp. 114-123, 145-149
219.Re
220.Rs
221.%A Mcilroy, P.M.
222.%T "Optimistic Sorting and Information Theoretic Complexity"
223.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
224.%V January 1992
225.Re
226.Rs
227.%A Bentley, J.L.
228.%T "Engineering a Sort Function"
229.%J "bentley@research.att.com"
230.%V January 1992
231.Re
232.Sh STANDARDS
233The
234.Fn qsort
235function
236conforms to
237.St -ansiC .
238