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.357
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 .
73The size of each object is specified by 74The size of each object is specified by
74.Fa size . 75.Fa size .
75.Fn Mergesort 76.Fn mergesort
76behaves similarly, but 77behaves similarly, but
77.Em requires 78.Em requires
78that 79that
@@ -106,51 +107,49 @@ is stable.
106.Pp 107.Pp
107The 108The
108.Fn qsort 109.Fn qsort
109function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, 110function is an implementation of C.A.R. Hoare's
111.Dq quicksort
112algorithm,
110a variant of partition-exchange sorting; in particular, see D.E. Knuth's 113a variant of partition-exchange sorting; in particular, see D.E. Knuth's
111Algorithm Q. 114Algorithm Q.
112.Fn Qsort 115.Fn qsort
113takes O N lg N average time. 116takes O N lg N average time.
114This implementation uses median selection to avoid its 117This implementation uses median selection to avoid its
115O N**2 worst-case behavior. 118O N**2 worst-case behavior.
116.Pp 119.Pp
117The 120The
118.Fn heapsort 121.Fn heapsort
119function is an implementation of J.W.J. William's ``heapsort'' algorithm, 122function is an implementation of J.W.J. William's
123.Dq heapsort
124algorithm,
120a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. 125a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
121.Fn Heapsort 126.Fn heapsort
122takes O N lg N worst-case time. 127takes O N lg N worst-case time.
123Its 128This implementation of
124.Em only
125advantage over
126.Fn qsort 129.Fn qsort
127is that it uses almost no additional memory; while 130is implemented without recursive function calls.
128.Fn qsort
129does not allocate memory, it is implemented using recursion.
130.Pp 131.Pp
131The function 132The function
132.Fn mergesort 133.Fn mergesort
133requires additional memory of size 134requires additional memory of size
134.Fa nmemb * 135.Fa nmemb *
135.Fa size 136.Fa size
136bytes; it should be used only when space is not at a premium. 137bytes; it should be used only when space is not at a premium.
137.Fn Mergesort 138.Fn mergesort
138is optimized for data with pre-existing order; its worst case 139is optimized for data with pre-existing order; its worst case
139time is O N lg N; its best case is O N. 140time is O N lg N; its best case is O N.
140.Pp 141.Pp
141Normally, 142Normally,
142.Fn qsort 143.Fn qsort
143is faster than 144is faster than
144.Fn mergesort 145.Fn mergesort ,
145is faster than 146which is faster than
146.Fn heapsort . 147.Fn heapsort .
147Memory availability and pre-existing order in the data can make this 148Memory availability and pre-existing order in the data can make this untrue.
148untrue.
149.Sh RETURN VALUES 149.Sh RETURN VALUES
150The 150The
151.Fn qsort 151.Fn qsort
152function 152function returns no value.
153returns no value.
154.Pp 153.Pp
155Upon successful completion, 154Upon successful completion,
156.Fn heapsort 155.Fn heapsort
@@ -163,20 +162,21 @@ is set to indicate the error.
163.Sh ERRORS 162.Sh ERRORS
164The 163The
165.Fn heapsort 164.Fn heapsort
166function succeeds unless: 165and
166.Fn mergesort
167functions succeed unless:
167.Bl -tag -width Er 168.Bl -tag -width Er
168.It Bq Er EINVAL 169.It Bq Er EINVAL
169The 170The
170.Fa size 171.Fa size
171argument is zero, or, 172argument is zero, or the
172the
173.Fa size 173.Fa size
174argument to 174argument to
175.Fn mergesort 175.Fn mergesort
176is less than 176is 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
180or 180or
181.Fn mergesort 181.Fn mergesort
182were unable to allocate memory. 182were unable to allocate memory.
@@ -185,7 +185,7 @@ were unable to allocate memory.
185Previous versions of 185Previous versions of
186.Fn qsort 186.Fn qsort
187did not permit the comparison routine itself to call 187did not permit the comparison routine itself to call
188.Fn qsort 3 . 188.Fn qsort .
189This is no longer true. 189This 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
230The 230The
231.Fn qsort 231.Fn qsort
232function 232function conforms to
233conforms to
234.St -ansiC . 233.St -ansiC .