diff options
Diffstat (limited to 'src/lib/libc/stdlib/qsort.3')
| -rw-r--r-- | src/lib/libc/stdlib/qsort.3 | 93 |
1 files changed, 46 insertions, 47 deletions
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 index eb122cde12..92c75d5365 100644 --- a/src/lib/libc/stdlib/qsort.3 +++ b/src/lib/libc/stdlib/qsort.3 | |||
| @@ -13,11 +13,7 @@ | |||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
| 14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
| 15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
| 16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
| 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 | 17 | .\" may be used to endorse or promote products derived from this software |
| 22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
| 23 | .\" | 19 | .\" |
| @@ -33,14 +29,15 @@ | |||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
| 35 | .\" | 31 | .\" |
| 36 | .\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 | 32 | .\" $OpenBSD: qsort.3,v 1.15 2007/05/31 19:19:31 jmc Exp $ |
| 37 | .\" $Id: qsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | 33 | .\" |
| 39 | .Dd June 4, 1993 | 34 | .Dd $Mdocdate: May 31 2007 $ |
| 40 | .Dt QSORT 3 | 35 | .Dt QSORT 3 |
| 41 | .Os | 36 | .Os |
| 42 | .Sh NAME | 37 | .Sh NAME |
| 43 | .Nm qsort, heapsort, mergesort | 38 | .Nm qsort , |
| 39 | .Nm heapsort , | ||
| 40 | .Nm mergesort | ||
| 44 | .Nd sort functions | 41 | .Nd sort functions |
| 45 | .Sh SYNOPSIS | 42 | .Sh SYNOPSIS |
| 46 | .Fd #include <stdlib.h> | 43 | .Fd #include <stdlib.h> |
| @@ -72,7 +69,7 @@ objects, the initial member of which is pointed to by | |||
| 72 | .Fa base . | 69 | .Fa base . |
| 73 | The size of each object is specified by | 70 | The size of each object is specified by |
| 74 | .Fa size . | 71 | .Fa size . |
| 75 | .Fn Mergesort | 72 | .Fn mergesort |
| 76 | behaves similarly, but | 73 | behaves similarly, but |
| 77 | .Em requires | 74 | .Em requires |
| 78 | that | 75 | that |
| @@ -106,51 +103,49 @@ is stable. | |||
| 106 | .Pp | 103 | .Pp |
| 107 | The | 104 | The |
| 108 | .Fn qsort | 105 | .Fn qsort |
| 109 | function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, | 106 | function is an implementation of C.A.R. Hoare's |
| 107 | .Dq quicksort | ||
| 108 | algorithm, | ||
| 110 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | 109 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's |
| 111 | Algorithm Q. | 110 | Algorithm Q. |
| 112 | .Fn Qsort | 111 | .Fn qsort |
| 113 | takes O N lg N average time. | 112 | takes O N lg N average time. |
| 114 | This implementation uses median selection to avoid its | 113 | This implementation uses median selection to avoid its |
| 115 | O N**2 worst-case behavior. | 114 | O N**2 worst-case behavior. |
| 116 | .Pp | 115 | .Pp |
| 117 | The | 116 | The |
| 118 | .Fn heapsort | 117 | .Fn heapsort |
| 119 | function is an implementation of J.W.J. William's ``heapsort'' algorithm, | 118 | function is an implementation of J.W.J. William's |
| 119 | .Dq heapsort | ||
| 120 | algorithm, | ||
| 120 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | 121 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. |
| 121 | .Fn Heapsort | 122 | .Fn heapsort |
| 122 | takes O N lg N worst-case time. | 123 | takes O N lg N worst-case time. |
| 123 | Its | 124 | This implementation of |
| 124 | .Em only | 125 | .Fn heapsort |
| 125 | advantage over | 126 | is implemented without recursive function calls. |
| 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 | 127 | .Pp |
| 131 | The function | 128 | The function |
| 132 | .Fn mergesort | 129 | .Fn mergesort |
| 133 | requires additional memory of size | 130 | requires additional memory of size |
| 134 | .Fa nmemb * | 131 | .Fa nmemb * |
| 135 | .Fa size | 132 | .Fa size |
| 136 | bytes; it should be used only when space is not at a premium. | 133 | bytes; it should be used only when space is not at a premium. |
| 137 | .Fn Mergesort | 134 | .Fn mergesort |
| 138 | is optimized for data with pre-existing order; its worst case | 135 | is optimized for data with pre-existing order; its worst case |
| 139 | time is O N lg N; its best case is O N. | 136 | time is O N lg N; its best case is O N. |
| 140 | .Pp | 137 | .Pp |
| 141 | Normally, | 138 | Normally, |
| 142 | .Fn qsort | 139 | .Fn qsort |
| 143 | is faster than | 140 | is faster than |
| 144 | .Fn mergesort | 141 | .Fn mergesort , |
| 145 | is faster than | 142 | which is faster than |
| 146 | .Fn heapsort . | 143 | .Fn heapsort . |
| 147 | Memory availability and pre-existing order in the data can make this | 144 | Memory availability and pre-existing order in the data can make this untrue. |
| 148 | untrue. | ||
| 149 | .Sh RETURN VALUES | 145 | .Sh RETURN VALUES |
| 150 | The | 146 | The |
| 151 | .Fn qsort | 147 | .Fn qsort |
| 152 | function | 148 | function returns no value. |
| 153 | returns no value. | ||
| 154 | .Pp | 149 | .Pp |
| 155 | Upon successful completion, | 150 | Upon successful completion, |
| 156 | .Fn heapsort | 151 | .Fn heapsort |
| @@ -163,30 +158,25 @@ is set to indicate the error. | |||
| 163 | .Sh ERRORS | 158 | .Sh ERRORS |
| 164 | The | 159 | The |
| 165 | .Fn heapsort | 160 | .Fn heapsort |
| 166 | function succeeds unless: | 161 | and |
| 162 | .Fn mergesort | ||
| 163 | functions succeed unless: | ||
| 167 | .Bl -tag -width Er | 164 | .Bl -tag -width Er |
| 168 | .It Bq Er EINVAL | 165 | .It Bq Er EINVAL |
| 169 | The | 166 | The |
| 170 | .Fa size | 167 | .Fa size |
| 171 | argument is zero, or, | 168 | argument is zero, or the |
| 172 | the | ||
| 173 | .Fa size | 169 | .Fa size |
| 174 | argument to | 170 | argument to |
| 175 | .Fn mergesort | 171 | .Fn mergesort |
| 176 | is less than | 172 | is less than |
| 177 | .Dq "sizeof(void *) / 2" . | 173 | .Dq "sizeof(void *) / 2" . |
| 178 | .It Bq Er ENOMEM | 174 | .It Bq Er ENOMEM |
| 179 | .Fn Heapsort | 175 | .Fn heapsort |
| 180 | or | 176 | or |
| 181 | .Fn mergesort | 177 | .Fn mergesort |
| 182 | were unable to allocate memory. | 178 | were unable to allocate memory. |
| 183 | .El | 179 | .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 | 180 | .Sh SEE ALSO |
| 191 | .Xr sort 1 , | 181 | .Xr sort 1 , |
| 192 | .Xr radixsort 3 | 182 | .Xr radixsort 3 |
| @@ -204,7 +194,7 @@ This is no longer true. | |||
| 204 | .%T "Heapsort" | 194 | .%T "Heapsort" |
| 205 | .%J "Communications of the ACM" | 195 | .%J "Communications of the ACM" |
| 206 | .%V 7:1 | 196 | .%V 7:1 |
| 207 | .%P pp. 347-348 | 197 | .%P pp. 347\-348 |
| 208 | .Re | 198 | .Re |
| 209 | .Rs | 199 | .Rs |
| 210 | .%A Knuth, D.E. | 200 | .%A Knuth, D.E. |
| @@ -212,23 +202,32 @@ This is no longer true. | |||
| 212 | .%B "The Art of Computer Programming" | 202 | .%B "The Art of Computer Programming" |
| 213 | .%V Vol. 3 | 203 | .%V Vol. 3 |
| 214 | .%T "Sorting and Searching" | 204 | .%T "Sorting and Searching" |
| 215 | .%P pp. 114-123, 145-149 | 205 | .%P pp. 114\-123, 145\-149 |
| 216 | .Re | 206 | .Re |
| 217 | .Rs | 207 | .Rs |
| 218 | .%A Mcilroy, P.M. | 208 | .%A McIlroy, P.M. |
| 219 | .%T "Optimistic Sorting and Information Theoretic Complexity" | 209 | .%T "Optimistic Sorting and Information Theoretic Complexity" |
| 220 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" | 210 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" |
| 221 | .%V January 1992 | 211 | .%P pp. 467\-464 |
| 212 | .%D January 1993 | ||
| 222 | .Re | 213 | .Re |
| 223 | .Rs | 214 | .Rs |
| 224 | .%A Bentley, J.L. | 215 | .%A Bentley, J.L. |
| 216 | .%A McIlroy, M.D. | ||
| 225 | .%T "Engineering a Sort Function" | 217 | .%T "Engineering a Sort Function" |
| 226 | .%J "bentley@research.att.com" | 218 | .%J "Software \- Practice and Experience" |
| 227 | .%V January 1992 | 219 | .%V Vol. 23(11) |
| 220 | .%P pp. 1249\-1265 | ||
| 221 | .%D November 1993 | ||
| 228 | .Re | 222 | .Re |
| 229 | .Sh STANDARDS | 223 | .Sh STANDARDS |
| 224 | Previous versions of | ||
| 225 | .Fn qsort | ||
| 226 | did not permit the comparison routine itself to call | ||
| 227 | .Fn qsort . | ||
| 228 | This is no longer true. | ||
| 229 | .Pp | ||
| 230 | The | 230 | The |
| 231 | .Fn qsort | 231 | .Fn qsort |
| 232 | function | 232 | function conforms to |
| 233 | conforms to | ||
| 234 | .St -ansiC . | 233 | .St -ansiC . |
