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.\" 395e24a424STim J. Robbins.Dd September 30, 2003 4058f0484fSRodney W. Grimes.Dt QSORT 3 4158f0484fSRodney W. Grimes.Os 4258f0484fSRodney W. Grimes.Sh NAME 43eca67d51SGarrett Wollman.Nm qsort , qsort_r , heapsort , mergesort 4458f0484fSRodney W. Grimes.Nd sort functions 4525bb73e0SAlexey Zelkin.Sh LIBRARY 4625bb73e0SAlexey Zelkin.Lb libc 4758f0484fSRodney W. Grimes.Sh SYNOPSIS 488aefde06SJeroen Ruigrok van der Werven.In stdlib.h 4958f0484fSRodney W. Grimes.Ft void 501798791dSRuslan Ermilov.Fo qsort 511798791dSRuslan Ermilov.Fa "void *base" 521798791dSRuslan Ermilov.Fa "size_t nmemb" 531798791dSRuslan Ermilov.Fa "size_t size" 541798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 551798791dSRuslan Ermilov.Fc 56eca67d51SGarrett Wollman.Ft void 57eca67d51SGarrett Wollman.Fo qsort_r 58eca67d51SGarrett Wollman.Fa "void *base" 59eca67d51SGarrett Wollman.Fa "size_t nmemb" 60eca67d51SGarrett Wollman.Fa "size_t size" 61eca67d51SGarrett Wollman.Fa "void *thunk" 621798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]void *, const void *, const void *\*[rp]" 63eca67d51SGarrett Wollman.Fc 6458f0484fSRodney W. Grimes.Ft int 651798791dSRuslan Ermilov.Fo heapsort 661798791dSRuslan Ermilov.Fa "void *base" 671798791dSRuslan Ermilov.Fa "size_t nmemb" 681798791dSRuslan Ermilov.Fa "size_t size" 691798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 701798791dSRuslan Ermilov.Fc 7158f0484fSRodney W. Grimes.Ft int 721798791dSRuslan Ermilov.Fo mergesort 731798791dSRuslan Ermilov.Fa "void *base" 741798791dSRuslan Ermilov.Fa "size_t nmemb" 751798791dSRuslan Ermilov.Fa "size_t size" 761798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 771798791dSRuslan Ermilov.Fc 7858f0484fSRodney W. Grimes.Sh DESCRIPTION 7958f0484fSRodney W. GrimesThe 8058f0484fSRodney W. Grimes.Fn qsort 8158f0484fSRodney W. Grimesfunction is a modified partition-exchange sort, or quicksort. 8258f0484fSRodney W. GrimesThe 8358f0484fSRodney W. Grimes.Fn heapsort 8458f0484fSRodney W. Grimesfunction is a modified selection sort. 8558f0484fSRodney W. GrimesThe 8658f0484fSRodney W. Grimes.Fn mergesort 8758f0484fSRodney W. Grimesfunction is a modified merge sort with exponential search 8858f0484fSRodney W. Grimesintended for sorting data with pre-existing order. 8958f0484fSRodney W. Grimes.Pp 9058f0484fSRodney W. GrimesThe 9158f0484fSRodney W. Grimes.Fn qsort 9258f0484fSRodney W. Grimesand 9358f0484fSRodney W. Grimes.Fn heapsort 9458f0484fSRodney W. Grimesfunctions sort an array of 9558f0484fSRodney W. Grimes.Fa nmemb 9658f0484fSRodney W. Grimesobjects, the initial member of which is pointed to by 9758f0484fSRodney W. Grimes.Fa base . 9858f0484fSRodney W. GrimesThe size of each object is specified by 9958f0484fSRodney W. Grimes.Fa size . 1001fae73b1SRuslan ErmilovThe 1011fae73b1SRuslan Ermilov.Fn mergesort 1021fae73b1SRuslan Ermilovfunction 10358f0484fSRodney W. Grimesbehaves similarly, but 10458f0484fSRodney W. Grimes.Em requires 10558f0484fSRodney W. Grimesthat 10658f0484fSRodney W. Grimes.Fa size 10758f0484fSRodney W. Grimesbe greater than 10858f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" . 10958f0484fSRodney W. Grimes.Pp 11058f0484fSRodney W. GrimesThe contents of the array 11158f0484fSRodney W. Grimes.Fa base 11258f0484fSRodney W. Grimesare sorted in ascending order according to 11358f0484fSRodney W. Grimesa comparison function pointed to by 11458f0484fSRodney W. Grimes.Fa compar , 11558f0484fSRodney W. Grimeswhich requires two arguments pointing to the objects being 11658f0484fSRodney W. Grimescompared. 11758f0484fSRodney W. Grimes.Pp 11858f0484fSRodney W. GrimesThe comparison function must return an integer less than, equal to, or 11958f0484fSRodney W. Grimesgreater than zero if the first argument is considered to be respectively 12058f0484fSRodney W. Grimesless than, equal to, or greater than the second. 12158f0484fSRodney W. Grimes.Pp 122eca67d51SGarrett WollmanThe 123eca67d51SGarrett Wollman.Fn qsort_r 124eca67d51SGarrett Wollmanfunction behaves identically to 125eca67d51SGarrett Wollman.Fn qsort , 126eca67d51SGarrett Wollmanexcept that it takes an additional argument, 127eca67d51SGarrett Wollman.Fa thunk , 128eca67d51SGarrett Wollmanwhich is passed unchanged as the first argument to function pointed to 129eca67d51SGarrett Wollman.Fa compar . 130eca67d51SGarrett WollmanThis allows the comparison function to access additional 131eca67d51SGarrett Wollmandata without using global variables, and thus 132eca67d51SGarrett Wollman.Fn qsort_r 133eca67d51SGarrett Wollmanis suitable for use in functions which must be reentrant. 134eca67d51SGarrett Wollman.Pp 135eca67d51SGarrett WollmanThe algorithms implemented by 136eca67d51SGarrett Wollman.Fn qsort , 137eca67d51SGarrett Wollman.Fn qsort_r , 13858f0484fSRodney W. Grimesand 13958f0484fSRodney W. Grimes.Fn heapsort 14058f0484fSRodney W. Grimesare 14158f0484fSRodney W. Grimes.Em not 14258f0484fSRodney W. Grimesstable, that is, if two members compare as equal, their order in 14358f0484fSRodney W. Grimesthe sorted array is undefined. 144eca67d51SGarrett WollmanThe 14558f0484fSRodney W. Grimes.Fn mergesort 146eca67d51SGarrett Wollmanalgorithm is stable. 14758f0484fSRodney W. Grimes.Pp 14858f0484fSRodney W. GrimesThe 14958f0484fSRodney W. Grimes.Fn qsort 150eca67d51SGarrett Wollmanand 151eca67d51SGarrett Wollman.Fn qsort_r 1521798791dSRuslan Ermilovfunctions are an implementation of C.A.R. Hoare's 1531798791dSRuslan Ermilov.Dq quicksort 1541798791dSRuslan Ermilovalgorithm, 15558f0484fSRodney W. Grimesa variant of partition-exchange sorting; in particular, see D.E. Knuth's 15658f0484fSRodney W. GrimesAlgorithm Q. 157eca67d51SGarrett Wollman.Sy Quicksort 15858f0484fSRodney W. Grimestakes O N lg N average time. 15958f0484fSRodney W. GrimesThis implementation uses median selection to avoid its 16058f0484fSRodney W. GrimesO N**2 worst-case behavior. 16158f0484fSRodney W. Grimes.Pp 16258f0484fSRodney W. GrimesThe 16358f0484fSRodney W. Grimes.Fn heapsort 1641798791dSRuslan Ermilovfunction is an implementation of J.W.J. William's 1651798791dSRuslan Ermilov.Dq heapsort 1661798791dSRuslan Ermilovalgorithm, 16758f0484fSRodney W. Grimesa variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. 168eca67d51SGarrett Wollman.Sy Heapsort 16958f0484fSRodney W. Grimestakes O N lg N worst-case time. 17058f0484fSRodney W. GrimesIts 17158f0484fSRodney W. Grimes.Em only 17258f0484fSRodney W. Grimesadvantage over 17358f0484fSRodney W. Grimes.Fn qsort 17458f0484fSRodney W. Grimesis that it uses almost no additional memory; while 17558f0484fSRodney W. Grimes.Fn qsort 17658f0484fSRodney W. Grimesdoes not allocate memory, it is implemented using recursion. 17758f0484fSRodney W. Grimes.Pp 17858f0484fSRodney W. GrimesThe function 17958f0484fSRodney W. Grimes.Fn mergesort 18058f0484fSRodney W. Grimesrequires additional memory of size 18158f0484fSRodney W. Grimes.Fa nmemb * 18258f0484fSRodney W. Grimes.Fa size 18358f0484fSRodney W. Grimesbytes; it should be used only when space is not at a premium. 1841fae73b1SRuslan ErmilovThe 1851fae73b1SRuslan Ermilov.Fn mergesort 1861fae73b1SRuslan Ermilovfunction 18758f0484fSRodney W. Grimesis optimized for data with pre-existing order; its worst case 18858f0484fSRodney W. Grimestime is O N lg N; its best case is O N. 18958f0484fSRodney W. Grimes.Pp 19058f0484fSRodney W. GrimesNormally, 19158f0484fSRodney W. Grimes.Fn qsort 19258f0484fSRodney W. Grimesis faster than 19358f0484fSRodney W. Grimes.Fn mergesort 19458f0484fSRodney W. Grimesis faster than 19558f0484fSRodney W. Grimes.Fn heapsort . 19658f0484fSRodney W. GrimesMemory availability and pre-existing order in the data can make this 19758f0484fSRodney W. Grimesuntrue. 19858f0484fSRodney W. Grimes.Sh RETURN VALUES 19958f0484fSRodney W. GrimesThe 20058f0484fSRodney W. Grimes.Fn qsort 201eca67d51SGarrett Wollmanand 202eca67d51SGarrett Wollman.Fn qsort_r 203eca67d51SGarrett Wollmanfunctions 204eca67d51SGarrett Wollmanreturn no value. 20558f0484fSRodney W. Grimes.Pp 206d6002fefSRuslan Ermilov.Rv -std heapsort mergesort 20758f0484fSRodney W. Grimes.Sh ERRORS 20858f0484fSRodney W. GrimesThe 20958f0484fSRodney W. Grimes.Fn heapsort 2108d2c3c32SRobert Nordierand 2118d2c3c32SRobert Nordier.Fn mergesort 2128d2c3c32SRobert Nordierfunctions succeed unless: 21358f0484fSRodney W. Grimes.Bl -tag -width Er 21458f0484fSRodney W. Grimes.It Bq Er EINVAL 21558f0484fSRodney W. GrimesThe 21658f0484fSRodney W. Grimes.Fa size 21758f0484fSRodney W. Grimesargument is zero, or, 21858f0484fSRodney W. Grimesthe 21958f0484fSRodney W. Grimes.Fa size 22058f0484fSRodney W. Grimesargument to 22158f0484fSRodney W. Grimes.Fn mergesort 22258f0484fSRodney W. Grimesis less than 22358f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" . 22458f0484fSRodney W. Grimes.It Bq Er ENOMEM 2251fae73b1SRuslan ErmilovThe 2261fae73b1SRuslan Ermilov.Fn heapsort 22758f0484fSRodney W. Grimesor 22858f0484fSRodney W. Grimes.Fn mergesort 2291fae73b1SRuslan Ermilovfunctions 23058f0484fSRodney W. Grimeswere unable to allocate memory. 23158f0484fSRodney W. Grimes.El 23258f0484fSRodney W. Grimes.Sh COMPATIBILITY 23358f0484fSRodney W. GrimesPrevious versions of 23458f0484fSRodney W. Grimes.Fn qsort 23558f0484fSRodney W. Grimesdid not permit the comparison routine itself to call 23658f0484fSRodney W. Grimes.Fn qsort 3 . 23758f0484fSRodney W. GrimesThis is no longer true. 23858f0484fSRodney W. Grimes.Sh SEE ALSO 23958f0484fSRodney W. Grimes.Xr sort 1 , 24058f0484fSRodney W. Grimes.Xr radixsort 3 24158f0484fSRodney W. Grimes.Rs 24258f0484fSRodney W. Grimes.%A Hoare, C.A.R. 24358f0484fSRodney W. Grimes.%D 1962 24458f0484fSRodney W. Grimes.%T "Quicksort" 24558f0484fSRodney W. Grimes.%J "The Computer Journal" 24658f0484fSRodney W. Grimes.%V 5:1 24758f0484fSRodney W. Grimes.%P pp. 10-15 24858f0484fSRodney W. Grimes.Re 24958f0484fSRodney W. Grimes.Rs 25058f0484fSRodney W. Grimes.%A Williams, J.W.J 25158f0484fSRodney W. Grimes.%D 1964 25258f0484fSRodney W. Grimes.%T "Heapsort" 25358f0484fSRodney W. Grimes.%J "Communications of the ACM" 25458f0484fSRodney W. Grimes.%V 7:1 25558f0484fSRodney W. Grimes.%P pp. 347-348 25658f0484fSRodney W. Grimes.Re 25758f0484fSRodney W. Grimes.Rs 25858f0484fSRodney W. Grimes.%A Knuth, D.E. 25958f0484fSRodney W. Grimes.%D 1968 26058f0484fSRodney W. Grimes.%B "The Art of Computer Programming" 26158f0484fSRodney W. Grimes.%V Vol. 3 26258f0484fSRodney W. Grimes.%T "Sorting and Searching" 26358f0484fSRodney W. Grimes.%P pp. 114-123, 145-149 26458f0484fSRodney W. Grimes.Re 26558f0484fSRodney W. Grimes.Rs 2665e24a424STim J. Robbins.%A McIlroy, P.M. 26758f0484fSRodney W. Grimes.%T "Optimistic Sorting and Information Theoretic Complexity" 26858f0484fSRodney W. Grimes.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" 26958f0484fSRodney W. Grimes.%V January 1992 27058f0484fSRodney W. Grimes.Re 27158f0484fSRodney W. Grimes.Rs 27258f0484fSRodney W. Grimes.%A Bentley, J.L. 2735e24a424STim J. Robbins.%A McIlroy, M.D. 27458f0484fSRodney W. Grimes.%T "Engineering a Sort Function" 2755e24a424STim J. Robbins.%J "Software--Practice and Experience" 2765e24a424STim J. Robbins.%V Vol. 23(11) 2775e24a424STim J. Robbins.%P pp. 1249-1265 2785e24a424STim J. Robbins.%D November\ 1993 27958f0484fSRodney W. Grimes.Re 28058f0484fSRodney W. Grimes.Sh STANDARDS 28158f0484fSRodney W. GrimesThe 28258f0484fSRodney W. Grimes.Fn qsort 28358f0484fSRodney W. Grimesfunction 28458f0484fSRodney W. Grimesconforms to 285588a200cSRuslan Ermilov.St -isoC . 286