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