xref: /freebsd/lib/libc/stdlib/qsort.3 (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
158f0484fSRodney W. Grimes.\" Copyright (c) 1990, 1991, 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" This code is derived from software contributed to Berkeley by
558f0484fSRodney W. Grimes.\" the American National Standards Committee X3, on Information
658f0484fSRodney W. Grimes.\" Processing Systems.
758f0484fSRodney W. Grimes.\"
858f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes.\" are met:
1158f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1858f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1958f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes.\"    without specific prior written permission.
2358f0484fSRodney W. Grimes.\"
2458f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3558f0484fSRodney W. Grimes.\"
3658f0484fSRodney W. Grimes.\"     @(#)qsort.3	8.1 (Berkeley) 6/4/93
377f3dea24SPeter Wemm.\" $FreeBSD$
3858f0484fSRodney W. Grimes.\"
3958f0484fSRodney W. Grimes.Dd June 4, 1993
4058f0484fSRodney W. Grimes.Dt QSORT 3
4158f0484fSRodney W. Grimes.Os
4258f0484fSRodney W. Grimes.Sh NAME
4358f0484fSRodney W. Grimes.Nm qsort, heapsort, mergesort
4458f0484fSRodney W. Grimes.Nd sort functions
4558f0484fSRodney W. Grimes.Sh SYNOPSIS
4658f0484fSRodney W. Grimes.Fd #include <stdlib.h>
4758f0484fSRodney W. Grimes.Ft void
4858f0484fSRodney W. Grimes.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
4958f0484fSRodney W. Grimes.Ft int
5058f0484fSRodney W. Grimes.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
5158f0484fSRodney W. Grimes.Ft int
5258f0484fSRodney W. Grimes.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
5358f0484fSRodney W. Grimes.Sh DESCRIPTION
5458f0484fSRodney W. GrimesThe
5558f0484fSRodney W. Grimes.Fn qsort
5658f0484fSRodney W. Grimesfunction is a modified partition-exchange sort, or quicksort.
5758f0484fSRodney W. GrimesThe
5858f0484fSRodney W. Grimes.Fn heapsort
5958f0484fSRodney W. Grimesfunction is a modified selection sort.
6058f0484fSRodney W. GrimesThe
6158f0484fSRodney W. Grimes.Fn mergesort
6258f0484fSRodney W. Grimesfunction is a modified merge sort with exponential search
6358f0484fSRodney W. Grimesintended for sorting data with pre-existing order.
6458f0484fSRodney W. Grimes.Pp
6558f0484fSRodney W. GrimesThe
6658f0484fSRodney W. Grimes.Fn qsort
6758f0484fSRodney W. Grimesand
6858f0484fSRodney W. Grimes.Fn heapsort
6958f0484fSRodney W. Grimesfunctions sort an array of
7058f0484fSRodney W. Grimes.Fa nmemb
7158f0484fSRodney W. Grimesobjects, the initial member of which is pointed to by
7258f0484fSRodney W. Grimes.Fa base .
7358f0484fSRodney W. GrimesThe size of each object is specified by
7458f0484fSRodney W. Grimes.Fa size .
7558f0484fSRodney W. Grimes.Fn Mergesort
7658f0484fSRodney W. Grimesbehaves similarly, but
7758f0484fSRodney W. Grimes.Em requires
7858f0484fSRodney W. Grimesthat
7958f0484fSRodney W. Grimes.Fa size
8058f0484fSRodney W. Grimesbe greater than
8158f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" .
8258f0484fSRodney W. Grimes.Pp
8358f0484fSRodney W. GrimesThe contents of the array
8458f0484fSRodney W. Grimes.Fa base
8558f0484fSRodney W. Grimesare sorted in ascending order according to
8658f0484fSRodney W. Grimesa comparison function pointed to by
8758f0484fSRodney W. Grimes.Fa compar ,
8858f0484fSRodney W. Grimeswhich requires two arguments pointing to the objects being
8958f0484fSRodney W. Grimescompared.
9058f0484fSRodney W. Grimes.Pp
9158f0484fSRodney W. GrimesThe comparison function must return an integer less than, equal to, or
9258f0484fSRodney W. Grimesgreater than zero if the first argument is considered to be respectively
9358f0484fSRodney W. Grimesless than, equal to, or greater than the second.
9458f0484fSRodney W. Grimes.Pp
9558f0484fSRodney W. GrimesThe functions
9658f0484fSRodney W. Grimes.Fn qsort
9758f0484fSRodney W. Grimesand
9858f0484fSRodney W. Grimes.Fn heapsort
9958f0484fSRodney W. Grimesare
10058f0484fSRodney W. Grimes.Em not
10158f0484fSRodney W. Grimesstable, that is, if two members compare as equal, their order in
10258f0484fSRodney W. Grimesthe sorted array is undefined.
10358f0484fSRodney W. GrimesThe function
10458f0484fSRodney W. Grimes.Fn mergesort
10558f0484fSRodney W. Grimesis stable.
10658f0484fSRodney W. Grimes.Pp
10758f0484fSRodney W. GrimesThe
10858f0484fSRodney W. Grimes.Fn qsort
10958f0484fSRodney W. Grimesfunction is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
11058f0484fSRodney W. Grimesa variant of partition-exchange sorting; in particular, see D.E. Knuth's
11158f0484fSRodney W. GrimesAlgorithm Q.
11258f0484fSRodney W. Grimes.Fn Qsort
11358f0484fSRodney W. Grimestakes O N lg N average time.
11458f0484fSRodney W. GrimesThis implementation uses median selection to avoid its
11558f0484fSRodney W. GrimesO N**2 worst-case behavior.
11658f0484fSRodney W. Grimes.Pp
11758f0484fSRodney W. GrimesThe
11858f0484fSRodney W. Grimes.Fn heapsort
11958f0484fSRodney W. Grimesfunction is an implementation of J.W.J. William's ``heapsort'' algorithm,
12058f0484fSRodney W. Grimesa variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
12158f0484fSRodney W. Grimes.Fn Heapsort
12258f0484fSRodney W. Grimestakes O N lg N worst-case time.
12358f0484fSRodney W. GrimesIts
12458f0484fSRodney W. Grimes.Em only
12558f0484fSRodney W. Grimesadvantage over
12658f0484fSRodney W. Grimes.Fn qsort
12758f0484fSRodney W. Grimesis that it uses almost no additional memory; while
12858f0484fSRodney W. Grimes.Fn qsort
12958f0484fSRodney W. Grimesdoes not allocate memory, it is implemented using recursion.
13058f0484fSRodney W. Grimes.Pp
13158f0484fSRodney W. GrimesThe function
13258f0484fSRodney W. Grimes.Fn mergesort
13358f0484fSRodney W. Grimesrequires additional memory of size
13458f0484fSRodney W. Grimes.Fa nmemb *
13558f0484fSRodney W. Grimes.Fa size
13658f0484fSRodney W. Grimesbytes; it should be used only when space is not at a premium.
13758f0484fSRodney W. Grimes.Fn Mergesort
13858f0484fSRodney W. Grimesis optimized for data with pre-existing order; its worst case
13958f0484fSRodney W. Grimestime is O N lg N; its best case is O N.
14058f0484fSRodney W. Grimes.Pp
14158f0484fSRodney W. GrimesNormally,
14258f0484fSRodney W. Grimes.Fn qsort
14358f0484fSRodney W. Grimesis faster than
14458f0484fSRodney W. Grimes.Fn mergesort
14558f0484fSRodney W. Grimesis faster than
14658f0484fSRodney W. Grimes.Fn heapsort .
14758f0484fSRodney W. GrimesMemory availability and pre-existing order in the data can make this
14858f0484fSRodney W. Grimesuntrue.
14958f0484fSRodney W. Grimes.Sh RETURN VALUES
15058f0484fSRodney W. GrimesThe
15158f0484fSRodney W. Grimes.Fn qsort
15258f0484fSRodney W. Grimesfunction
15358f0484fSRodney W. Grimesreturns no value.
15458f0484fSRodney W. Grimes.Pp
15558f0484fSRodney W. GrimesUpon successful completion,
15658f0484fSRodney W. Grimes.Fn heapsort
15758f0484fSRodney W. Grimesand
15858f0484fSRodney W. Grimes.Fn mergesort
15958f0484fSRodney W. Grimesreturn 0.
16058f0484fSRodney W. GrimesOtherwise, they return \-1 and the global variable
16158f0484fSRodney W. Grimes.Va errno
16258f0484fSRodney W. Grimesis set to indicate the error.
16358f0484fSRodney W. Grimes.Sh ERRORS
16458f0484fSRodney W. GrimesThe
16558f0484fSRodney W. Grimes.Fn heapsort
1668d2c3c32SRobert Nordierand
1678d2c3c32SRobert Nordier.Fn mergesort
1688d2c3c32SRobert Nordierfunctions succeed unless:
16958f0484fSRodney W. Grimes.Bl -tag -width Er
17058f0484fSRodney W. Grimes.It Bq Er EINVAL
17158f0484fSRodney W. GrimesThe
17258f0484fSRodney W. Grimes.Fa size
17358f0484fSRodney W. Grimesargument is zero, or,
17458f0484fSRodney W. Grimesthe
17558f0484fSRodney W. Grimes.Fa size
17658f0484fSRodney W. Grimesargument to
17758f0484fSRodney W. Grimes.Fn mergesort
17858f0484fSRodney W. Grimesis less than
17958f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" .
18058f0484fSRodney W. Grimes.It Bq Er ENOMEM
18158f0484fSRodney W. Grimes.Fn Heapsort
18258f0484fSRodney W. Grimesor
18358f0484fSRodney W. Grimes.Fn mergesort
18458f0484fSRodney W. Grimeswere unable to allocate memory.
18558f0484fSRodney W. Grimes.El
18658f0484fSRodney W. Grimes.Sh COMPATIBILITY
18758f0484fSRodney W. GrimesPrevious versions of
18858f0484fSRodney W. Grimes.Fn qsort
18958f0484fSRodney W. Grimesdid not permit the comparison routine itself to call
19058f0484fSRodney W. Grimes.Fn qsort 3 .
19158f0484fSRodney W. GrimesThis is no longer true.
19258f0484fSRodney W. Grimes.Sh SEE ALSO
19358f0484fSRodney W. Grimes.Xr sort 1 ,
19458f0484fSRodney W. Grimes.Xr radixsort 3
19558f0484fSRodney W. Grimes.Rs
19658f0484fSRodney W. Grimes.%A Hoare, C.A.R.
19758f0484fSRodney W. Grimes.%D 1962
19858f0484fSRodney W. Grimes.%T "Quicksort"
19958f0484fSRodney W. Grimes.%J "The Computer Journal"
20058f0484fSRodney W. Grimes.%V 5:1
20158f0484fSRodney W. Grimes.%P pp. 10-15
20258f0484fSRodney W. Grimes.Re
20358f0484fSRodney W. Grimes.Rs
20458f0484fSRodney W. Grimes.%A Williams, J.W.J
20558f0484fSRodney W. Grimes.%D 1964
20658f0484fSRodney W. Grimes.%T "Heapsort"
20758f0484fSRodney W. Grimes.%J "Communications of the ACM"
20858f0484fSRodney W. Grimes.%V 7:1
20958f0484fSRodney W. Grimes.%P pp. 347-348
21058f0484fSRodney W. Grimes.Re
21158f0484fSRodney W. Grimes.Rs
21258f0484fSRodney W. Grimes.%A Knuth, D.E.
21358f0484fSRodney W. Grimes.%D 1968
21458f0484fSRodney W. Grimes.%B "The Art of Computer Programming"
21558f0484fSRodney W. Grimes.%V Vol. 3
21658f0484fSRodney W. Grimes.%T "Sorting and Searching"
21758f0484fSRodney W. Grimes.%P pp. 114-123, 145-149
21858f0484fSRodney W. Grimes.Re
21958f0484fSRodney W. Grimes.Rs
22058f0484fSRodney W. Grimes.%A Mcilroy, P.M.
22158f0484fSRodney W. Grimes.%T "Optimistic Sorting and Information Theoretic Complexity"
22258f0484fSRodney W. Grimes.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
22358f0484fSRodney W. Grimes.%V January 1992
22458f0484fSRodney W. Grimes.Re
22558f0484fSRodney W. Grimes.Rs
22658f0484fSRodney W. Grimes.%A Bentley, J.L.
22758f0484fSRodney W. Grimes.%T "Engineering a Sort Function"
22858f0484fSRodney W. Grimes.%J "bentley@research.att.com"
22958f0484fSRodney W. Grimes.%V January 1992
23058f0484fSRodney W. Grimes.Re
23158f0484fSRodney W. Grimes.Sh STANDARDS
23258f0484fSRodney W. GrimesThe
23358f0484fSRodney W. Grimes.Fn qsort
23458f0484fSRodney W. Grimesfunction
23558f0484fSRodney W. Grimesconforms to
23658f0484fSRodney W. Grimes.St -ansiC .
237