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..6c4eba46bf
--- /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.9 2002/02/23 19:51:46 miod 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.
128This implementation of
129.Fn qsort
130is implemented without recursive function calls.
131.Pp
132The function
133.Fn mergesort
134requires additional memory of size
135.Fa nmemb *
136.Fa size
137bytes; it should be used only when space is not at a premium.
138.Fn mergesort
139is optimized for data with pre-existing order; its worst case
140time is O N lg N; its best case is O N.
141.Pp
142Normally,
143.Fn qsort
144is faster than
145.Fn mergesort ,
146which is faster than
147.Fn heapsort .
148Memory availability and pre-existing order in the data can make this untrue.
149.Sh RETURN VALUES
150The
151.Fn qsort
152function returns 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
165and
166.Fn mergesort
167functions succeed unless:
168.Bl -tag -width Er
169.It Bq Er EINVAL
170The
171.Fa size
172argument is zero, or the
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 conforms to
233.St -ansiC .