diff options
author | deraadt <> | 1995-10-18 08:42:23 +0000 |
---|---|---|
committer | deraadt <> | 1995-10-18 08:42:23 +0000 |
commit | 0527d29da443886d92e9a418180c5b25a5f8d270 (patch) | |
tree | 86b3a64928451a669cefa27900e5884036b4e349 /src/lib/libc/stdlib/qsort.3 | |
download | openbsd-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.3 | 234 |
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 | ||
54 | The | ||
55 | .Fn qsort | ||
56 | function is a modified partition-exchange sort, or quicksort. | ||
57 | The | ||
58 | .Fn heapsort | ||
59 | function is a modified selection sort. | ||
60 | The | ||
61 | .Fn mergesort | ||
62 | function is a modified merge sort with exponential search | ||
63 | intended for sorting data with pre-existing order. | ||
64 | .Pp | ||
65 | The | ||
66 | .Fn qsort | ||
67 | and | ||
68 | .Fn heapsort | ||
69 | functions sort an array of | ||
70 | .Fa nmemb | ||
71 | objects, the initial member of which is pointed to by | ||
72 | .Fa base . | ||
73 | The size of each object is specified by | ||
74 | .Fa size . | ||
75 | .Fn Mergesort | ||
76 | behaves similarly, but | ||
77 | .Em requires | ||
78 | that | ||
79 | .Fa size | ||
80 | be greater than | ||
81 | .Dq "sizeof(void *) / 2" . | ||
82 | .Pp | ||
83 | The contents of the array | ||
84 | .Fa base | ||
85 | are sorted in ascending order according to | ||
86 | a comparison function pointed to by | ||
87 | .Fa compar , | ||
88 | which requires two arguments pointing to the objects being | ||
89 | compared. | ||
90 | .Pp | ||
91 | The comparison function must return an integer less than, equal to, or | ||
92 | greater than zero if the first argument is considered to be respectively | ||
93 | less than, equal to, or greater than the second. | ||
94 | .Pp | ||
95 | The functions | ||
96 | .Fn qsort | ||
97 | and | ||
98 | .Fn heapsort | ||
99 | are | ||
100 | .Em not | ||
101 | stable, that is, if two members compare as equal, their order in | ||
102 | the sorted array is undefined. | ||
103 | The function | ||
104 | .Fn mergesort | ||
105 | is stable. | ||
106 | .Pp | ||
107 | The | ||
108 | .Fn qsort | ||
109 | function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, | ||
110 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | ||
111 | Algorithm Q. | ||
112 | .Fn Qsort | ||
113 | takes O N lg N average time. | ||
114 | This implementation uses median selection to avoid its | ||
115 | O N**2 worst-case behavior. | ||
116 | .Pp | ||
117 | The | ||
118 | .Fn heapsort | ||
119 | function is an implementation of J.W.J. William's ``heapsort'' algorithm, | ||
120 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | ||
121 | .Fn Heapsort | ||
122 | takes O N lg N worst-case time. | ||
123 | Its | ||
124 | .Em only | ||
125 | advantage over | ||
126 | .Fn qsort | ||
127 | is that it uses almost no additional memory; while | ||
128 | .Fn qsort | ||
129 | does not allocate memory, it is implemented using recursion. | ||
130 | .Pp | ||
131 | The function | ||
132 | .Fn mergesort | ||
133 | requires additional memory of size | ||
134 | .Fa nmemb * | ||
135 | .Fa size | ||
136 | bytes; it should be used only when space is not at a premium. | ||
137 | .Fn Mergesort | ||
138 | is optimized for data with pre-existing order; its worst case | ||
139 | time is O N lg N; its best case is O N. | ||
140 | .Pp | ||
141 | Normally, | ||
142 | .Fn qsort | ||
143 | is faster than | ||
144 | .Fn mergesort | ||
145 | is faster than | ||
146 | .Fn heapsort . | ||
147 | Memory availability and pre-existing order in the data can make this | ||
148 | untrue. | ||
149 | .Sh RETURN VALUES | ||
150 | The | ||
151 | .Fn qsort | ||
152 | function | ||
153 | returns no value. | ||
154 | .Pp | ||
155 | Upon successful completion, | ||
156 | .Fn heapsort | ||
157 | and | ||
158 | .Fn mergesort | ||
159 | return 0. | ||
160 | Otherwise, they return \-1 and the global variable | ||
161 | .Va errno | ||
162 | is set to indicate the error. | ||
163 | .Sh ERRORS | ||
164 | The | ||
165 | .Fn heapsort | ||
166 | function succeeds unless: | ||
167 | .Bl -tag -width Er | ||
168 | .It Bq Er EINVAL | ||
169 | The | ||
170 | .Fa size | ||
171 | argument is zero, or, | ||
172 | the | ||
173 | .Fa size | ||
174 | argument to | ||
175 | .Fn mergesort | ||
176 | is less than | ||
177 | .Dq "sizeof(void *) / 2" . | ||
178 | .It Bq Er ENOMEM | ||
179 | .Fn Heapsort | ||
180 | or | ||
181 | .Fn mergesort | ||
182 | were unable to allocate memory. | ||
183 | .El | ||
184 | .Sh COMPATIBILITY | ||
185 | Previous versions of | ||
186 | .Fn qsort | ||
187 | did not permit the comparison routine itself to call | ||
188 | .Fn qsort 3 . | ||
189 | This 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 | ||
230 | The | ||
231 | .Fn qsort | ||
232 | function | ||
233 | conforms to | ||
234 | .St -ansiC . | ||