1*0a6a1f1dSLionel Sambuc.\" $NetBSD: qsort.3,v 1.14 2014/09/19 16:02:58 wiz Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1990, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" This code is derived from software contributed to Berkeley by 72fe8fb19SBen Gras.\" the American National Standards Committee X3, on Information 82fe8fb19SBen Gras.\" Processing Systems. 92fe8fb19SBen Gras.\" 102fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 112fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 122fe8fb19SBen Gras.\" are met: 132fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 142fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 152fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 162fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 172fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 182fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 192fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 202fe8fb19SBen Gras.\" without specific prior written permission. 212fe8fb19SBen Gras.\" 222fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 232fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 242fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 252fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 262fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 272fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 282fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 292fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 302fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 312fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 322fe8fb19SBen Gras.\" SUCH DAMAGE. 332fe8fb19SBen Gras.\" 342fe8fb19SBen Gras.\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 352fe8fb19SBen Gras.\" 362fe8fb19SBen Gras.Dd June 4, 1993 372fe8fb19SBen Gras.Dt QSORT 3 382fe8fb19SBen Gras.Os 392fe8fb19SBen Gras.Sh NAME 402fe8fb19SBen Gras.Nm qsort , 412fe8fb19SBen Gras.Nm heapsort , 422fe8fb19SBen Gras.Nm mergesort 432fe8fb19SBen Gras.Nd sort functions 442fe8fb19SBen Gras.Sh LIBRARY 452fe8fb19SBen Gras.Lb libc 462fe8fb19SBen Gras.Sh SYNOPSIS 472fe8fb19SBen Gras.In stdlib.h 482fe8fb19SBen Gras.Ft void 492fe8fb19SBen Gras.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" 502fe8fb19SBen Gras.Ft int 512fe8fb19SBen Gras.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" 522fe8fb19SBen Gras.Ft int 532fe8fb19SBen Gras.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" 542fe8fb19SBen Gras.Sh DESCRIPTION 552fe8fb19SBen GrasThe 562fe8fb19SBen Gras.Fn qsort 572fe8fb19SBen Grasfunction is a modified partition-exchange sort, or quicksort. 582fe8fb19SBen GrasThe 592fe8fb19SBen Gras.Fn heapsort 602fe8fb19SBen Grasfunction is a modified selection sort. 612fe8fb19SBen GrasThe 622fe8fb19SBen Gras.Fn mergesort 632fe8fb19SBen Grasfunction is a modified merge sort with exponential search 642fe8fb19SBen Grasintended for sorting data with pre-existing order. 652fe8fb19SBen Gras.Pp 662fe8fb19SBen GrasThe 672fe8fb19SBen Gras.Fn qsort 682fe8fb19SBen Grasand 692fe8fb19SBen Gras.Fn heapsort 702fe8fb19SBen Grasfunctions sort an array of 712fe8fb19SBen Gras.Fa nmemb 722fe8fb19SBen Grasobjects, the initial member of which is pointed to by 732fe8fb19SBen Gras.Fa base . 742fe8fb19SBen GrasThe size of each object is specified by 752fe8fb19SBen Gras.Fa size . 762fe8fb19SBen Gras.Fn mergesort 772fe8fb19SBen Grasbehaves similarly, but 782fe8fb19SBen Gras.Em requires 792fe8fb19SBen Grasthat 802fe8fb19SBen Gras.Fa size 812fe8fb19SBen Grasbe greater than 822fe8fb19SBen Gras.Dq "sizeof(void *) / 2" . 832fe8fb19SBen Gras.Pp 842fe8fb19SBen GrasThe contents of the array 852fe8fb19SBen Gras.Fa base 862fe8fb19SBen Grasare sorted in ascending order according to 872fe8fb19SBen Grasa comparison function pointed to by 882fe8fb19SBen Gras.Fa compar , 892fe8fb19SBen Graswhich requires two arguments pointing to the objects being 902fe8fb19SBen Grascompared. 912fe8fb19SBen Gras.Pp 922fe8fb19SBen GrasThe comparison function must return an integer less than, equal to, or 932fe8fb19SBen Grasgreater than zero if the first argument is considered to be respectively 942fe8fb19SBen Grasless than, equal to, or greater than the second. 952fe8fb19SBen Gras.Pp 962fe8fb19SBen GrasThe functions 972fe8fb19SBen Gras.Fn qsort 982fe8fb19SBen Grasand 992fe8fb19SBen Gras.Fn heapsort 1002fe8fb19SBen Grasare 1012fe8fb19SBen Gras.Em not 1022fe8fb19SBen Grasstable, that is, if two members compare as equal, their order in 1032fe8fb19SBen Grasthe sorted array is undefined. 1042fe8fb19SBen GrasThe function 1052fe8fb19SBen Gras.Fn mergesort 1062fe8fb19SBen Grasis stable. 1072fe8fb19SBen Gras.Pp 1082fe8fb19SBen GrasThe 1092fe8fb19SBen Gras.Fn qsort 1102fe8fb19SBen Grasfunction is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, 1112fe8fb19SBen Grasa variant of partition-exchange sorting; in particular, see D.E. Knuth's 1122fe8fb19SBen GrasAlgorithm Q. 1132fe8fb19SBen Gras.Fn qsort 1142fe8fb19SBen Grastakes O N lg N average time. 1152fe8fb19SBen GrasThis implementation uses median selection to avoid its 1162fe8fb19SBen GrasO N**2 worst-case behavior. 1172fe8fb19SBen Gras.Pp 1182fe8fb19SBen GrasThe 1192fe8fb19SBen Gras.Fn heapsort 1202fe8fb19SBen Grasfunction is an implementation of J.W.J. William's ``heapsort'' algorithm, 1212fe8fb19SBen Grasa variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. 1222fe8fb19SBen Gras.Fn heapsort 1232fe8fb19SBen Grastakes O N lg N worst-case time. 1242fe8fb19SBen GrasIts 1252fe8fb19SBen Gras.Em only 1262fe8fb19SBen Grasadvantage over 1272fe8fb19SBen Gras.Fn qsort 1282fe8fb19SBen Grasis that it uses almost no additional memory; while 1292fe8fb19SBen Gras.Fn qsort 1302fe8fb19SBen Grasdoes not allocate memory, it is implemented using recursion. 1312fe8fb19SBen Gras.Pp 1322fe8fb19SBen GrasThe function 1332fe8fb19SBen Gras.Fn mergesort 1342fe8fb19SBen Grasrequires additional memory of size 1352fe8fb19SBen Gras.Fa nmemb * 1362fe8fb19SBen Gras.Fa size 1372fe8fb19SBen Grasbytes; it should be used only when space is not at a premium. 1382fe8fb19SBen Gras.Fn mergesort 1392fe8fb19SBen Grasis optimized for data with pre-existing order; its worst case 1402fe8fb19SBen Grastime is O N lg N; its best case is O N. 1412fe8fb19SBen Gras.Pp 1422fe8fb19SBen GrasNormally, 1432fe8fb19SBen Gras.Fn qsort 1442fe8fb19SBen Grasis faster than 1452fe8fb19SBen Gras.Fn mergesort 1462fe8fb19SBen Grasis faster than 1472fe8fb19SBen Gras.Fn heapsort . 1482fe8fb19SBen GrasMemory availability and pre-existing order in the data can make this 1492fe8fb19SBen Grasuntrue. 1502fe8fb19SBen Gras.Sh RETURN VALUES 1512fe8fb19SBen GrasThe 1522fe8fb19SBen Gras.Fn qsort 1532fe8fb19SBen Grasfunction 1542fe8fb19SBen Grasreturns no value. 1552fe8fb19SBen Gras.Pp 1562fe8fb19SBen GrasUpon successful completion, 1572fe8fb19SBen Gras.Fn heapsort 1582fe8fb19SBen Grasand 1592fe8fb19SBen Gras.Fn mergesort 1602fe8fb19SBen Grasreturn 0. 1612fe8fb19SBen GrasOtherwise, they return \-1 and the global variable 1622fe8fb19SBen Gras.Va errno 1632fe8fb19SBen Grasis set to indicate the error. 164*0a6a1f1dSLionel Sambuc.Sh COMPATIBILITY 165*0a6a1f1dSLionel SambucPrevious versions of 166*0a6a1f1dSLionel Sambuc.Fn qsort 167*0a6a1f1dSLionel Sambucdid not permit the comparison routine itself to call 168*0a6a1f1dSLionel Sambuc.Fn qsort . 169*0a6a1f1dSLionel SambucThis is no longer true. 1702fe8fb19SBen Gras.Sh ERRORS 1712fe8fb19SBen GrasThe 1722fe8fb19SBen Gras.Fn heapsort 1732fe8fb19SBen Grasfunction succeeds unless: 1742fe8fb19SBen Gras.Bl -tag -width Er 1752fe8fb19SBen Gras.It Bq Er EINVAL 1762fe8fb19SBen GrasThe 1772fe8fb19SBen Gras.Fa size 1782fe8fb19SBen Grasargument is zero, or, 1792fe8fb19SBen Grasthe 1802fe8fb19SBen Gras.Fa size 1812fe8fb19SBen Grasargument to 1822fe8fb19SBen Gras.Fn mergesort 1832fe8fb19SBen Grasis less than 1842fe8fb19SBen Gras.Dq "sizeof(void *) / 2" . 1852fe8fb19SBen Gras.It Bq Er ENOMEM 1862fe8fb19SBen Gras.Fn heapsort 1872fe8fb19SBen Grasor 1882fe8fb19SBen Gras.Fn mergesort 1892fe8fb19SBen Graswere unable to allocate memory. 1902fe8fb19SBen Gras.El 1912fe8fb19SBen Gras.Sh SEE ALSO 1922fe8fb19SBen Gras.Xr sort 1 , 1932fe8fb19SBen Gras.Xr radixsort 3 1942fe8fb19SBen Gras.Rs 1952fe8fb19SBen Gras.%A Hoare, C.A.R. 1962fe8fb19SBen Gras.%D 1962 1972fe8fb19SBen Gras.%T "Quicksort" 1982fe8fb19SBen Gras.%J "The Computer Journal" 1992fe8fb19SBen Gras.%V 5:1 2002fe8fb19SBen Gras.%P pp. 10-15 2012fe8fb19SBen Gras.Re 2022fe8fb19SBen Gras.Rs 2032fe8fb19SBen Gras.%A Williams, J.W.J 2042fe8fb19SBen Gras.%D 1964 2052fe8fb19SBen Gras.%T "Heapsort" 2062fe8fb19SBen Gras.%J "Communications of the ACM" 2072fe8fb19SBen Gras.%V 7:1 2082fe8fb19SBen Gras.%P pp. 347-348 2092fe8fb19SBen Gras.Re 2102fe8fb19SBen Gras.Rs 2112fe8fb19SBen Gras.%A Knuth, D.E. 2122fe8fb19SBen Gras.%D 1968 2132fe8fb19SBen Gras.%B "The Art of Computer Programming" 2142fe8fb19SBen Gras.%V Vol. 3 2152fe8fb19SBen Gras.%T "Sorting and Searching" 2162fe8fb19SBen Gras.%P pp. 114-123, 145-149 2172fe8fb19SBen Gras.Re 2182fe8fb19SBen Gras.Rs 2192fe8fb19SBen Gras.%A McIlroy, P.M. 2202fe8fb19SBen Gras.%D 1993 2212fe8fb19SBen Gras.%T "Optimistic Sorting and Information Theoretic Complexity" 2222fe8fb19SBen Gras.%J "Proceedings of the Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" 2232fe8fb19SBen Gras.%P pp. 467-474 2242fe8fb19SBen Gras.Re 2252fe8fb19SBen Gras.Rs 2262fe8fb19SBen Gras.%A Bentley, J.L. and McIlroy, M.D. 2272fe8fb19SBen Gras.%D 1993 2282fe8fb19SBen Gras.%T "Engineering a Sort Function" 2292fe8fb19SBen Gras.%J "Software-Practice and Experience" 2302fe8fb19SBen Gras.%V Vol. 23 2312fe8fb19SBen Gras.%P pp. 1249-1265 2322fe8fb19SBen Gras.Re 2332fe8fb19SBen Gras.Sh STANDARDS 2342fe8fb19SBen GrasThe 2352fe8fb19SBen Gras.Fn qsort 2362fe8fb19SBen Grasfunction 2372fe8fb19SBen Grasconforms to 2382fe8fb19SBen Gras.St -ansiC . 239