This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / lib / libc / stdlib / qsort.3
CommitLineData
15637ed4
RG
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" 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.\" @(#)qsort.3 6.7 (Berkeley) 6/29/91
37.\"
38.Dd June 29, 1991
39.Dt QSORT 3
40.Os
41.Sh NAME
42.Nm qsort, heapsort
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.Sh DESCRIPTION
51The
52.Fn qsort
53function is a modified partition-exchange sort, or quicksort.
54The
55.Fn heapsort
56function is a modified selection sort.
57.Pp
58The
59.Fn qsort
60and
61.Fn heapsort
62functions sort an array of
63.Fa nmemb
64objects, the initial member of which is pointed to by
65.Fa base .
66The size of each object is specified by
67.Fa size .
68.Pp
69The contents of the array are sorted in ascending order according to
70a comparison function pointed to by
71.Fa compar ,
72which is called with two arguments that point to the objects being
73compared.
74.Pp
75The comparison function must return an integer less than, equal to, or
76greater than zero if the first argument is considered to be respectively
77less than, equal to, or greater than the second.
78.Pp
79The functions
80.Fn qsort
81and
82.Fn heapsort
83are
84.Em not
85stable, that is, if two members compare as equal, their order in
86the sorted array is undefined.
87.Pp
88The
89.Fn qsort
90function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
91a variant of partition-exchange sorting; in particular, see D.E. Knuth's
92Algorithm Q.
93.Fn Qsort
94takes O N lg N average time.
95This implementation uses median selection to avoid the traditional
96O N**2 worst-case behavior.
97.Pp
98The
99.Fn heapsort
100function is an implementation of J.W.J. William's ``heapsort'' algorithm,
101a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
102.Fn Heapsort
103takes O N lg N worst-case time.
104Its
105.Em only
106advantage over
107.Fn qsort
108is that it uses no additional memory.
109.Sh RETURN VALUES
110The
111.Fn qsort
112function
113returns no value.
114.Pp
115Upon successful completion,
116.Fn heapsort
117returns 0.
118Otherwise, it returns \-1 and the global variable
119.Va errno
120is set to indicate the error.
121.Sh ERRORS
122The
123.Fn heapsort
124function succeeds unless:
125.Bl -tag -width Er
126.It Bq Er EINVAL
127The
128.Fa size
129argument is zero.
130.Sh COMPATIBILITY
131Previous versions of
132.Fn qsort
133did not permit the comparison routine to itself call
134.Fn qsort 3 .
135This is no longer true.
136.Sh SEE ALSO
137.Xr sort 1 ,
138.Xr radixsort 3
139.Rs
140.%A Hoare, C.A.R.
141.%D 1962
142.%T "Quicksort"
143.%J "The Computer Journal"
144.%V 5:1
145.%P pp. 10-15
146.Re
147.Rs
148.%A Williams, J.W.J
149.%D 1964
150.%T "Heapsort"
151.%J "Communications of the ACM"
152.%V 7:1
153.%P pp. 347-348
154.Re
155.Rs
156.%A Knuth, D.E.
157.%D 1968
158.%B "The Art of Computer Programming"
159.%V Vol. 3
160.%T "Sorting and Searching"
161.%P pp. 114-123, 145-149
162.Re
163.Sh STANDARDS
164The
165.Fn qsort
166function
167conforms to
168.St -ansiC .