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