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