xref: /netbsd-src/lib/libc/stdlib/qsort.3 (revision 2a65137f552b06844d6d4647074580ad3b40d634)
1*2a65137fSwiz.\"	$NetBSD: qsort.3,v 1.14 2014/09/19 16:02:58 wiz Exp $
26dda330eSthorpej.\"
32f86deeaSmycroft.\" Copyright (c) 1990, 1991, 1993
42f86deeaSmycroft.\"	The Regents of the University of California.  All rights reserved.
561f28255Scgd.\"
661f28255Scgd.\" This code is derived from software contributed to Berkeley by
761f28255Scgd.\" the American National Standards Committee X3, on Information
861f28255Scgd.\" Processing Systems.
961f28255Scgd.\"
1061f28255Scgd.\" Redistribution and use in source and binary forms, with or without
1161f28255Scgd.\" modification, are permitted provided that the following conditions
1261f28255Scgd.\" are met:
1361f28255Scgd.\" 1. Redistributions of source code must retain the above copyright
1461f28255Scgd.\"    notice, this list of conditions and the following disclaimer.
1561f28255Scgd.\" 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd.\"    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd.\"    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc.\" 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd.\"    may be used to endorse or promote products derived from this software
2061f28255Scgd.\"    without specific prior written permission.
2161f28255Scgd.\"
2261f28255Scgd.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd.\" SUCH DAMAGE.
3361f28255Scgd.\"
342f86deeaSmycroft.\"     from: @(#)qsort.3	8.1 (Berkeley) 6/4/93
3561f28255Scgd.\"
362f86deeaSmycroft.Dd June 4, 1993
3761f28255Scgd.Dt QSORT 3
3861f28255Scgd.Os
3961f28255Scgd.Sh NAME
404e252e8cSmrg.Nm qsort ,
414e252e8cSmrg.Nm heapsort ,
424e252e8cSmrg.Nm mergesort
4361f28255Scgd.Nd sort functions
44312aca53Sperry.Sh LIBRARY
45312aca53Sperry.Lb libc
4661f28255Scgd.Sh SYNOPSIS
47472351e1Swiz.In stdlib.h
4861f28255Scgd.Ft void
4961f28255Scgd.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
5061f28255Scgd.Ft int
5161f28255Scgd.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
522f86deeaSmycroft.Ft int
532f86deeaSmycroft.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
5461f28255Scgd.Sh DESCRIPTION
5561f28255ScgdThe
5661f28255Scgd.Fn qsort
5761f28255Scgdfunction is a modified partition-exchange sort, or quicksort.
5861f28255ScgdThe
5961f28255Scgd.Fn heapsort
6061f28255Scgdfunction is a modified selection sort.
612f86deeaSmycroftThe
622f86deeaSmycroft.Fn mergesort
632f86deeaSmycroftfunction is a modified merge sort with exponential search
642f86deeaSmycroftintended for sorting data with pre-existing order.
6561f28255Scgd.Pp
6661f28255ScgdThe
6761f28255Scgd.Fn qsort
6861f28255Scgdand
6961f28255Scgd.Fn heapsort
7061f28255Scgdfunctions sort an array of
7161f28255Scgd.Fa nmemb
7261f28255Scgdobjects, the initial member of which is pointed to by
7361f28255Scgd.Fa base .
7461f28255ScgdThe size of each object is specified by
7561f28255Scgd.Fa size .
76ce83c69eSlukem.Fn mergesort
772f86deeaSmycroftbehaves similarly, but
782f86deeaSmycroft.Em requires
792f86deeaSmycroftthat
802f86deeaSmycroft.Fa size
812f86deeaSmycroftbe greater than
822f86deeaSmycroft.Dq "sizeof(void *) / 2" .
8361f28255Scgd.Pp
842f86deeaSmycroftThe contents of the array
852f86deeaSmycroft.Fa base
862f86deeaSmycroftare sorted in ascending order according to
8761f28255Scgda comparison function pointed to by
8861f28255Scgd.Fa compar ,
892f86deeaSmycroftwhich requires two arguments pointing to the objects being
9061f28255Scgdcompared.
9161f28255Scgd.Pp
9261f28255ScgdThe comparison function must return an integer less than, equal to, or
9361f28255Scgdgreater than zero if the first argument is considered to be respectively
9461f28255Scgdless than, equal to, or greater than the second.
9561f28255Scgd.Pp
9661f28255ScgdThe functions
9761f28255Scgd.Fn qsort
9861f28255Scgdand
9961f28255Scgd.Fn heapsort
10061f28255Scgdare
10161f28255Scgd.Em not
10261f28255Scgdstable, that is, if two members compare as equal, their order in
10361f28255Scgdthe sorted array is undefined.
1042f86deeaSmycroftThe function
1052f86deeaSmycroft.Fn mergesort
1062f86deeaSmycroftis stable.
10761f28255Scgd.Pp
10861f28255ScgdThe
10961f28255Scgd.Fn qsort
11061f28255Scgdfunction is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
11161f28255Scgda variant of partition-exchange sorting; in particular, see D.E. Knuth's
11261f28255ScgdAlgorithm Q.
113ce83c69eSlukem.Fn qsort
11461f28255Scgdtakes O N lg N average time.
1152f86deeaSmycroftThis implementation uses median selection to avoid its
11661f28255ScgdO N**2 worst-case behavior.
11761f28255Scgd.Pp
11861f28255ScgdThe
11961f28255Scgd.Fn heapsort
12061f28255Scgdfunction is an implementation of J.W.J. William's ``heapsort'' algorithm,
12161f28255Scgda variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
122ce83c69eSlukem.Fn heapsort
12361f28255Scgdtakes O N lg N worst-case time.
12461f28255ScgdIts
12561f28255Scgd.Em only
12661f28255Scgdadvantage over
12761f28255Scgd.Fn qsort
1282f86deeaSmycroftis that it uses almost no additional memory; while
1292f86deeaSmycroft.Fn qsort
1302f86deeaSmycroftdoes not allocate memory, it is implemented using recursion.
1312f86deeaSmycroft.Pp
1322f86deeaSmycroftThe function
1332f86deeaSmycroft.Fn mergesort
1342f86deeaSmycroftrequires additional memory of size
1352f86deeaSmycroft.Fa nmemb *
1362f86deeaSmycroft.Fa size
1372f86deeaSmycroftbytes; it should be used only when space is not at a premium.
138ce83c69eSlukem.Fn mergesort
1392f86deeaSmycroftis optimized for data with pre-existing order; its worst case
1402f86deeaSmycrofttime is O N lg N; its best case is O N.
1412f86deeaSmycroft.Pp
1422f86deeaSmycroftNormally,
1432f86deeaSmycroft.Fn qsort
1442f86deeaSmycroftis faster than
1452f86deeaSmycroft.Fn mergesort
1462f86deeaSmycroftis faster than
1472f86deeaSmycroft.Fn heapsort .
1482f86deeaSmycroftMemory availability and pre-existing order in the data can make this
1492f86deeaSmycroftuntrue.
15061f28255Scgd.Sh RETURN VALUES
15161f28255ScgdThe
15261f28255Scgd.Fn qsort
15361f28255Scgdfunction
15461f28255Scgdreturns no value.
15561f28255Scgd.Pp
15661f28255ScgdUpon successful completion,
15761f28255Scgd.Fn heapsort
1582f86deeaSmycroftand
1592f86deeaSmycroft.Fn mergesort
1602f86deeaSmycroftreturn 0.
1612f86deeaSmycroftOtherwise, they return \-1 and the global variable
16261f28255Scgd.Va errno
16361f28255Scgdis set to indicate the error.
164*2a65137fSwiz.Sh COMPATIBILITY
165*2a65137fSwizPrevious versions of
166*2a65137fSwiz.Fn qsort
167*2a65137fSwizdid not permit the comparison routine itself to call
168*2a65137fSwiz.Fn qsort .
169*2a65137fSwizThis is no longer true.
17061f28255Scgd.Sh ERRORS
17161f28255ScgdThe
17261f28255Scgd.Fn heapsort
17361f28255Scgdfunction succeeds unless:
17461f28255Scgd.Bl -tag -width Er
17561f28255Scgd.It Bq Er EINVAL
17661f28255ScgdThe
17761f28255Scgd.Fa size
1782f86deeaSmycroftargument is zero, or,
1792f86deeaSmycroftthe
1802f86deeaSmycroft.Fa size
1812f86deeaSmycroftargument to
1822f86deeaSmycroft.Fn mergesort
1832f86deeaSmycroftis less than
1842f86deeaSmycroft.Dq "sizeof(void *) / 2" .
1852f86deeaSmycroft.It Bq Er ENOMEM
186ce83c69eSlukem.Fn heapsort
1872f86deeaSmycroftor
1882f86deeaSmycroft.Fn mergesort
1892f86deeaSmycroftwere unable to allocate memory.
1902f86deeaSmycroft.El
19161f28255Scgd.Sh SEE ALSO
19261f28255Scgd.Xr sort 1 ,
19361f28255Scgd.Xr radixsort 3
19461f28255Scgd.Rs
19561f28255Scgd.%A Hoare, C.A.R.
19661f28255Scgd.%D 1962
19761f28255Scgd.%T "Quicksort"
19861f28255Scgd.%J "The Computer Journal"
19961f28255Scgd.%V 5:1
20061f28255Scgd.%P pp. 10-15
20161f28255Scgd.Re
20261f28255Scgd.Rs
20361f28255Scgd.%A Williams, J.W.J
20461f28255Scgd.%D 1964
20561f28255Scgd.%T "Heapsort"
20661f28255Scgd.%J "Communications of the ACM"
20761f28255Scgd.%V 7:1
20861f28255Scgd.%P pp. 347-348
20961f28255Scgd.Re
21061f28255Scgd.Rs
21161f28255Scgd.%A Knuth, D.E.
21261f28255Scgd.%D 1968
21361f28255Scgd.%B "The Art of Computer Programming"
21461f28255Scgd.%V Vol. 3
21561f28255Scgd.%T "Sorting and Searching"
21661f28255Scgd.%P pp. 114-123, 145-149
21761f28255Scgd.Re
2182f86deeaSmycroft.Rs
219077e910eSheinz.%A McIlroy, P.M.
220077e910eSheinz.%D 1993
2212f86deeaSmycroft.%T "Optimistic Sorting and Information Theoretic Complexity"
222077e910eSheinz.%J "Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
223077e910eSheinz.%P pp. 467-474
2242f86deeaSmycroft.Re
2252f86deeaSmycroft.Rs
226077e910eSheinz.%A Bentley, J.L. and McIlroy, M.D.
227077e910eSheinz.%D 1993
2282f86deeaSmycroft.%T "Engineering a Sort Function"
229077e910eSheinz.%J "Software-Practice and Experience"
230077e910eSheinz.%V Vol. 23
231077e910eSheinz.%P pp. 1249-1265
2322f86deeaSmycroft.Re
23361f28255Scgd.Sh STANDARDS
23461f28255ScgdThe
23561f28255Scgd.Fn qsort
23661f28255Scgdfunction
23761f28255Scgdconforms to
23861f28255Scgd.St -ansiC .
239