Deleted Added
full compact
qsort.3 (165903) qsort.3 (247014)
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.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
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.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93
33.\" $FreeBSD: head/lib/libc/stdlib/qsort.3 165903 2007-01-09 00:28:16Z imp $
33.\" $FreeBSD: head/lib/libc/stdlib/qsort.3 247014 2013-02-19 23:57:39Z keramida $
34.\"
34.\"
35.Dd September 30, 2003
35.Dd February 20, 2013
36.Dt QSORT 3
37.Os
38.Sh NAME
39.Nm qsort , qsort_r , heapsort , mergesort
40.Nd sort functions
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft void
46.Fo qsort
47.Fa "void *base"
48.Fa "size_t nmemb"
49.Fa "size_t size"
50.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
51.Fc
52.Ft void
53.Fo qsort_r
54.Fa "void *base"
55.Fa "size_t nmemb"
56.Fa "size_t size"
57.Fa "void *thunk"
58.Fa "int \*[lp]*compar\*[rp]\*[lp]void *, const void *, const void *\*[rp]"
59.Fc
60.Ft int
61.Fo heapsort
62.Fa "void *base"
63.Fa "size_t nmemb"
64.Fa "size_t size"
65.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
66.Fc
67.Ft int
68.Fo mergesort
69.Fa "void *base"
70.Fa "size_t nmemb"
71.Fa "size_t size"
72.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
73.Fc
74.Sh DESCRIPTION
75The
76.Fn qsort
77function is a modified partition-exchange sort, or quicksort.
78The
79.Fn heapsort
80function is a modified selection sort.
81The
82.Fn mergesort
83function is a modified merge sort with exponential search
84intended for sorting data with pre-existing order.
85.Pp
86The
87.Fn qsort
88and
89.Fn heapsort
90functions sort an array of
91.Fa nmemb
92objects, the initial member of which is pointed to by
93.Fa base .
94The size of each object is specified by
95.Fa size .
96The
97.Fn mergesort
98function
99behaves similarly, but
100.Em requires
101that
102.Fa size
103be greater than
104.Dq "sizeof(void *) / 2" .
105.Pp
106The contents of the array
107.Fa base
108are sorted in ascending order according to
109a comparison function pointed to by
110.Fa compar ,
111which requires two arguments pointing to the objects being
112compared.
113.Pp
114The comparison function must return an integer less than, equal to, or
115greater than zero if the first argument is considered to be respectively
116less than, equal to, or greater than the second.
117.Pp
118The
119.Fn qsort_r
120function behaves identically to
121.Fn qsort ,
122except that it takes an additional argument,
123.Fa thunk ,
124which is passed unchanged as the first argument to function pointed to
125.Fa compar .
126This allows the comparison function to access additional
127data without using global variables, and thus
128.Fn qsort_r
129is suitable for use in functions which must be reentrant.
130.Pp
131The algorithms implemented by
132.Fn qsort ,
133.Fn qsort_r ,
134and
135.Fn heapsort
136are
137.Em not
138stable, that is, if two members compare as equal, their order in
139the sorted array is undefined.
140The
141.Fn mergesort
142algorithm is stable.
143.Pp
144The
145.Fn qsort
146and
147.Fn qsort_r
148functions are an implementation of C.A.R.
149Hoare's
150.Dq quicksort
151algorithm,
152a variant of partition-exchange sorting; in particular, see
153.An D.E. Knuth Ns 's
154.%T "Algorithm Q" .
155.Sy Quicksort
156takes O N lg N average time.
157This implementation uses median selection to avoid its
158O N**2 worst-case behavior.
159.Pp
160The
161.Fn heapsort
162function is an implementation of
163.An "J.W.J. William" Ns 's
164.Dq heapsort
165algorithm,
166a variant of selection sorting; in particular, see
167.An "D.E. Knuth" Ns 's
168.%T "Algorithm H" .
169.Sy Heapsort
170takes O N lg N worst-case time.
171Its
172.Em only
173advantage over
174.Fn qsort
175is that it uses almost no additional memory; while
176.Fn qsort
177does not allocate memory, it is implemented using recursion.
178.Pp
179The function
180.Fn mergesort
181requires additional memory of size
182.Fa nmemb *
183.Fa size
184bytes; it should be used only when space is not at a premium.
185The
186.Fn mergesort
187function
188is optimized for data with pre-existing order; its worst case
189time is O N lg N; its best case is O N.
190.Pp
191Normally,
192.Fn qsort
193is faster than
194.Fn mergesort
195is faster than
196.Fn heapsort .
197Memory availability and pre-existing order in the data can make this
198untrue.
199.Sh RETURN VALUES
200The
201.Fn qsort
202and
203.Fn qsort_r
204functions
205return no value.
206.Pp
207.Rv -std heapsort mergesort
208.Sh COMPATIBILITY
209Previous versions of
210.Fn qsort
211did not permit the comparison routine itself to call
212.Fn qsort 3 .
213This is no longer true.
36.Dt QSORT 3
37.Os
38.Sh NAME
39.Nm qsort , qsort_r , heapsort , mergesort
40.Nd sort functions
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft void
46.Fo qsort
47.Fa "void *base"
48.Fa "size_t nmemb"
49.Fa "size_t size"
50.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
51.Fc
52.Ft void
53.Fo qsort_r
54.Fa "void *base"
55.Fa "size_t nmemb"
56.Fa "size_t size"
57.Fa "void *thunk"
58.Fa "int \*[lp]*compar\*[rp]\*[lp]void *, const void *, const void *\*[rp]"
59.Fc
60.Ft int
61.Fo heapsort
62.Fa "void *base"
63.Fa "size_t nmemb"
64.Fa "size_t size"
65.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
66.Fc
67.Ft int
68.Fo mergesort
69.Fa "void *base"
70.Fa "size_t nmemb"
71.Fa "size_t size"
72.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
73.Fc
74.Sh DESCRIPTION
75The
76.Fn qsort
77function is a modified partition-exchange sort, or quicksort.
78The
79.Fn heapsort
80function is a modified selection sort.
81The
82.Fn mergesort
83function is a modified merge sort with exponential search
84intended for sorting data with pre-existing order.
85.Pp
86The
87.Fn qsort
88and
89.Fn heapsort
90functions sort an array of
91.Fa nmemb
92objects, the initial member of which is pointed to by
93.Fa base .
94The size of each object is specified by
95.Fa size .
96The
97.Fn mergesort
98function
99behaves similarly, but
100.Em requires
101that
102.Fa size
103be greater than
104.Dq "sizeof(void *) / 2" .
105.Pp
106The contents of the array
107.Fa base
108are sorted in ascending order according to
109a comparison function pointed to by
110.Fa compar ,
111which requires two arguments pointing to the objects being
112compared.
113.Pp
114The comparison function must return an integer less than, equal to, or
115greater than zero if the first argument is considered to be respectively
116less than, equal to, or greater than the second.
117.Pp
118The
119.Fn qsort_r
120function behaves identically to
121.Fn qsort ,
122except that it takes an additional argument,
123.Fa thunk ,
124which is passed unchanged as the first argument to function pointed to
125.Fa compar .
126This allows the comparison function to access additional
127data without using global variables, and thus
128.Fn qsort_r
129is suitable for use in functions which must be reentrant.
130.Pp
131The algorithms implemented by
132.Fn qsort ,
133.Fn qsort_r ,
134and
135.Fn heapsort
136are
137.Em not
138stable, that is, if two members compare as equal, their order in
139the sorted array is undefined.
140The
141.Fn mergesort
142algorithm is stable.
143.Pp
144The
145.Fn qsort
146and
147.Fn qsort_r
148functions are an implementation of C.A.R.
149Hoare's
150.Dq quicksort
151algorithm,
152a variant of partition-exchange sorting; in particular, see
153.An D.E. Knuth Ns 's
154.%T "Algorithm Q" .
155.Sy Quicksort
156takes O N lg N average time.
157This implementation uses median selection to avoid its
158O N**2 worst-case behavior.
159.Pp
160The
161.Fn heapsort
162function is an implementation of
163.An "J.W.J. William" Ns 's
164.Dq heapsort
165algorithm,
166a variant of selection sorting; in particular, see
167.An "D.E. Knuth" Ns 's
168.%T "Algorithm H" .
169.Sy Heapsort
170takes O N lg N worst-case time.
171Its
172.Em only
173advantage over
174.Fn qsort
175is that it uses almost no additional memory; while
176.Fn qsort
177does not allocate memory, it is implemented using recursion.
178.Pp
179The function
180.Fn mergesort
181requires additional memory of size
182.Fa nmemb *
183.Fa size
184bytes; it should be used only when space is not at a premium.
185The
186.Fn mergesort
187function
188is optimized for data with pre-existing order; its worst case
189time is O N lg N; its best case is O N.
190.Pp
191Normally,
192.Fn qsort
193is faster than
194.Fn mergesort
195is faster than
196.Fn heapsort .
197Memory availability and pre-existing order in the data can make this
198untrue.
199.Sh RETURN VALUES
200The
201.Fn qsort
202and
203.Fn qsort_r
204functions
205return no value.
206.Pp
207.Rv -std heapsort mergesort
208.Sh COMPATIBILITY
209Previous versions of
210.Fn qsort
211did not permit the comparison routine itself to call
212.Fn qsort 3 .
213This is no longer true.
214.Sh EXAMPLES
215A sample program that sorts an array of
216.Vt int
217values in place using
218.Fn qsort ,
219and then prints the sorted array to standard output is:
220.Bd -literal
221#include <stdio.h>
222#include <stdlib.h>
223#include <string.h>
224
225/*
226 * Custom comparison function that can compare 'int' values through pointers
227 * passed by qsort(3).
228 */
229static int
230int_compare(const void *p1, const void *p2)
231{
232 int *left = (int *)p1;
233 int *right = (int *)p2;
234
235 if (*left < *right)
236 return (-1);
237 else if (*left > *right)
238 return (1);
239 else
240 return (0);
241}
242
243/*
244 * Sort an array of 'int' values and print it to standard output.
245 */
246int
247main(void)
248{
249 int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 };
250 const int array_size = sizeof(int_array) / sizeof(int_array[0]);
251 int k;
252
253 qsort(&int_array, array_size, sizeof(int), int_compare);
254 for (k = 0; k < array_size; k++)
255 printf(" %d", int_array[k]);
256 printf("\\n");
257 exit(EXIT_SUCCESS);
258}
259.Ed
214.Sh ERRORS
215The
216.Fn heapsort
217and
218.Fn mergesort
219functions succeed unless:
220.Bl -tag -width Er
221.It Bq Er EINVAL
222The
223.Fa size
224argument is zero, or,
225the
226.Fa size
227argument to
228.Fn mergesort
229is less than
230.Dq "sizeof(void *) / 2" .
231.It Bq Er ENOMEM
232The
233.Fn heapsort
234or
235.Fn mergesort
236functions
237were unable to allocate memory.
238.El
239.Sh SEE ALSO
240.Xr sort 1 ,
241.Xr radixsort 3
242.Rs
243.%A Hoare, C.A.R.
244.%D 1962
245.%T "Quicksort"
246.%J "The Computer Journal"
247.%V 5:1
248.%P pp. 10-15
249.Re
250.Rs
251.%A Williams, J.W.J
252.%D 1964
253.%T "Heapsort"
254.%J "Communications of the ACM"
255.%V 7:1
256.%P pp. 347-348
257.Re
258.Rs
259.%A Knuth, D.E.
260.%D 1968
261.%B "The Art of Computer Programming"
262.%V Vol. 3
263.%T "Sorting and Searching"
264.%P pp. 114-123, 145-149
265.Re
266.Rs
267.%A McIlroy, P.M.
268.%T "Optimistic Sorting and Information Theoretic Complexity"
269.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
270.%V January 1992
271.Re
272.Rs
273.%A Bentley, J.L.
274.%A McIlroy, M.D.
275.%T "Engineering a Sort Function"
276.%J "Software--Practice and Experience"
277.%V Vol. 23(11)
278.%P pp. 1249-1265
279.%D November\ 1993
280.Re
281.Sh STANDARDS
282The
283.Fn qsort
284function
285conforms to
286.St -isoC .
260.Sh ERRORS
261The
262.Fn heapsort
263and
264.Fn mergesort
265functions succeed unless:
266.Bl -tag -width Er
267.It Bq Er EINVAL
268The
269.Fa size
270argument is zero, or,
271the
272.Fa size
273argument to
274.Fn mergesort
275is less than
276.Dq "sizeof(void *) / 2" .
277.It Bq Er ENOMEM
278The
279.Fn heapsort
280or
281.Fn mergesort
282functions
283were unable to allocate memory.
284.El
285.Sh SEE ALSO
286.Xr sort 1 ,
287.Xr radixsort 3
288.Rs
289.%A Hoare, C.A.R.
290.%D 1962
291.%T "Quicksort"
292.%J "The Computer Journal"
293.%V 5:1
294.%P pp. 10-15
295.Re
296.Rs
297.%A Williams, J.W.J
298.%D 1964
299.%T "Heapsort"
300.%J "Communications of the ACM"
301.%V 7:1
302.%P pp. 347-348
303.Re
304.Rs
305.%A Knuth, D.E.
306.%D 1968
307.%B "The Art of Computer Programming"
308.%V Vol. 3
309.%T "Sorting and Searching"
310.%P pp. 114-123, 145-149
311.Re
312.Rs
313.%A McIlroy, P.M.
314.%T "Optimistic Sorting and Information Theoretic Complexity"
315.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
316.%V January 1992
317.Re
318.Rs
319.%A Bentley, J.L.
320.%A McIlroy, M.D.
321.%T "Engineering a Sort Function"
322.%J "Software--Practice and Experience"
323.%V Vol. 23(11)
324.%P pp. 1249-1265
325.%D November\ 1993
326.Re
327.Sh STANDARDS
328The
329.Fn qsort
330function
331conforms to
332.St -isoC .