diff options
Diffstat (limited to 'src/lib/libc/stdlib/qsort.3')
| -rw-r--r-- | src/lib/libc/stdlib/qsort.3 | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 index eb122cde12..a0561cadbe 100644 --- a/src/lib/libc/stdlib/qsort.3 +++ b/src/lib/libc/stdlib/qsort.3 | |||
| @@ -33,14 +33,15 @@ | |||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | .\" SUCH DAMAGE. | 34 | .\" SUCH DAMAGE. |
| 35 | .\" | 35 | .\" |
| 36 | .\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 | 36 | .\" $OpenBSD: qsort.3,v 1.10 2003/05/10 06:48:30 jmc Exp $ |
| 37 | .\" $Id: qsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | 37 | .\" |
| 39 | .Dd June 4, 1993 | 38 | .Dd June 4, 1993 |
| 40 | .Dt QSORT 3 | 39 | .Dt QSORT 3 |
| 41 | .Os | 40 | .Os |
| 42 | .Sh NAME | 41 | .Sh NAME |
| 43 | .Nm qsort, heapsort, mergesort | 42 | .Nm qsort , |
| 43 | .Nm heapsort , | ||
| 44 | .Nm mergesort | ||
| 44 | .Nd sort functions | 45 | .Nd sort functions |
| 45 | .Sh SYNOPSIS | 46 | .Sh SYNOPSIS |
| 46 | .Fd #include <stdlib.h> | 47 | .Fd #include <stdlib.h> |
| @@ -72,7 +73,7 @@ objects, the initial member of which is pointed to by | |||
| 72 | .Fa base . | 73 | .Fa base . |
| 73 | The size of each object is specified by | 74 | The size of each object is specified by |
| 74 | .Fa size . | 75 | .Fa size . |
| 75 | .Fn Mergesort | 76 | .Fn mergesort |
| 76 | behaves similarly, but | 77 | behaves similarly, but |
| 77 | .Em requires | 78 | .Em requires |
| 78 | that | 79 | that |
| @@ -106,51 +107,49 @@ is stable. | |||
| 106 | .Pp | 107 | .Pp |
| 107 | The | 108 | The |
| 108 | .Fn qsort | 109 | .Fn qsort |
| 109 | function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, | 110 | function is an implementation of C.A.R. Hoare's |
| 111 | .Dq quicksort | ||
| 112 | algorithm, | ||
| 110 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | 113 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's |
| 111 | Algorithm Q. | 114 | Algorithm Q. |
| 112 | .Fn Qsort | 115 | .Fn qsort |
| 113 | takes O N lg N average time. | 116 | takes O N lg N average time. |
| 114 | This implementation uses median selection to avoid its | 117 | This implementation uses median selection to avoid its |
| 115 | O N**2 worst-case behavior. | 118 | O N**2 worst-case behavior. |
| 116 | .Pp | 119 | .Pp |
| 117 | The | 120 | The |
| 118 | .Fn heapsort | 121 | .Fn heapsort |
| 119 | function is an implementation of J.W.J. William's ``heapsort'' algorithm, | 122 | function is an implementation of J.W.J. William's |
| 123 | .Dq heapsort | ||
| 124 | algorithm, | ||
| 120 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | 125 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. |
| 121 | .Fn Heapsort | 126 | .Fn heapsort |
| 122 | takes O N lg N worst-case time. | 127 | takes O N lg N worst-case time. |
| 123 | Its | 128 | This implementation of |
| 124 | .Em only | ||
| 125 | advantage over | ||
| 126 | .Fn qsort | 129 | .Fn qsort |
| 127 | is that it uses almost no additional memory; while | 130 | is implemented without recursive function calls. |
| 128 | .Fn qsort | ||
| 129 | does not allocate memory, it is implemented using recursion. | ||
| 130 | .Pp | 131 | .Pp |
| 131 | The function | 132 | The function |
| 132 | .Fn mergesort | 133 | .Fn mergesort |
| 133 | requires additional memory of size | 134 | requires additional memory of size |
| 134 | .Fa nmemb * | 135 | .Fa nmemb * |
| 135 | .Fa size | 136 | .Fa size |
| 136 | bytes; it should be used only when space is not at a premium. | 137 | bytes; it should be used only when space is not at a premium. |
| 137 | .Fn Mergesort | 138 | .Fn mergesort |
| 138 | is optimized for data with pre-existing order; its worst case | 139 | 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 | time is O N lg N; its best case is O N. |
| 140 | .Pp | 141 | .Pp |
| 141 | Normally, | 142 | Normally, |
| 142 | .Fn qsort | 143 | .Fn qsort |
| 143 | is faster than | 144 | is faster than |
| 144 | .Fn mergesort | 145 | .Fn mergesort , |
| 145 | is faster than | 146 | which is faster than |
| 146 | .Fn heapsort . | 147 | .Fn heapsort . |
| 147 | Memory availability and pre-existing order in the data can make this | 148 | Memory availability and pre-existing order in the data can make this untrue. |
| 148 | untrue. | ||
| 149 | .Sh RETURN VALUES | 149 | .Sh RETURN VALUES |
| 150 | The | 150 | The |
| 151 | .Fn qsort | 151 | .Fn qsort |
| 152 | function | 152 | function returns no value. |
| 153 | returns no value. | ||
| 154 | .Pp | 153 | .Pp |
| 155 | Upon successful completion, | 154 | Upon successful completion, |
| 156 | .Fn heapsort | 155 | .Fn heapsort |
| @@ -163,20 +162,21 @@ is set to indicate the error. | |||
| 163 | .Sh ERRORS | 162 | .Sh ERRORS |
| 164 | The | 163 | The |
| 165 | .Fn heapsort | 164 | .Fn heapsort |
| 166 | function succeeds unless: | 165 | and |
| 166 | .Fn mergesort | ||
| 167 | functions succeed unless: | ||
| 167 | .Bl -tag -width Er | 168 | .Bl -tag -width Er |
| 168 | .It Bq Er EINVAL | 169 | .It Bq Er EINVAL |
| 169 | The | 170 | The |
| 170 | .Fa size | 171 | .Fa size |
| 171 | argument is zero, or, | 172 | argument is zero, or the |
| 172 | the | ||
| 173 | .Fa size | 173 | .Fa size |
| 174 | argument to | 174 | argument to |
| 175 | .Fn mergesort | 175 | .Fn mergesort |
| 176 | is less than | 176 | is less than |
| 177 | .Dq "sizeof(void *) / 2" . | 177 | .Dq "sizeof(void *) / 2" . |
| 178 | .It Bq Er ENOMEM | 178 | .It Bq Er ENOMEM |
| 179 | .Fn Heapsort | 179 | .Fn heapsort |
| 180 | or | 180 | or |
| 181 | .Fn mergesort | 181 | .Fn mergesort |
| 182 | were unable to allocate memory. | 182 | were unable to allocate memory. |
| @@ -185,7 +185,7 @@ were unable to allocate memory. | |||
| 185 | Previous versions of | 185 | Previous versions of |
| 186 | .Fn qsort | 186 | .Fn qsort |
| 187 | did not permit the comparison routine itself to call | 187 | did not permit the comparison routine itself to call |
| 188 | .Fn qsort 3 . | 188 | .Fn qsort . |
| 189 | This is no longer true. | 189 | This is no longer true. |
| 190 | .Sh SEE ALSO | 190 | .Sh SEE ALSO |
| 191 | .Xr sort 1 , | 191 | .Xr sort 1 , |
| @@ -229,6 +229,5 @@ This is no longer true. | |||
| 229 | .Sh STANDARDS | 229 | .Sh STANDARDS |
| 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 . |
