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. 16580b4d18SEd Maste.\" 3. Neither the name of the University nor the names of its contributors 1758f0484fSRodney W. Grimes.\" may be used to endorse or promote products derived from this software 1858f0484fSRodney W. Grimes.\" without specific prior written permission. 1958f0484fSRodney W. Grimes.\" 2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3058f0484fSRodney W. Grimes.\" SUCH DAMAGE. 3158f0484fSRodney W. Grimes.\" 3258f0484fSRodney W. Grimes.\" @(#)qsort.3 8.1 (Berkeley) 6/4/93 337f3dea24SPeter Wemm.\" $FreeBSD$ 3458f0484fSRodney W. Grimes.\" 35*27bb0d33SHans Petter Selasky.Dd April 19, 2023 3658f0484fSRodney W. Grimes.Dt QSORT 3 3758f0484fSRodney W. Grimes.Os 3858f0484fSRodney W. Grimes.Sh NAME 39604f1c41SEdward Tomasz Napierala.Nm qsort , 40604f1c41SEdward Tomasz Napierala.Nm qsort_b , 41604f1c41SEdward Tomasz Napierala.Nm qsort_r , 42604f1c41SEdward Tomasz Napierala.Nm heapsort , 43604f1c41SEdward Tomasz Napierala.Nm heapsort_b , 44604f1c41SEdward Tomasz Napierala.Nm mergesort , 45604f1c41SEdward Tomasz Napierala.Nm mergesort_b 4658f0484fSRodney W. Grimes.Nd sort functions 4725bb73e0SAlexey Zelkin.Sh LIBRARY 4825bb73e0SAlexey Zelkin.Lb libc 4958f0484fSRodney W. Grimes.Sh SYNOPSIS 508aefde06SJeroen Ruigrok van der Werven.In stdlib.h 5158f0484fSRodney W. Grimes.Ft void 521798791dSRuslan Ermilov.Fo qsort 531798791dSRuslan Ermilov.Fa "void *base" 541798791dSRuslan Ermilov.Fa "size_t nmemb" 551798791dSRuslan Ermilov.Fa "size_t size" 561798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 571798791dSRuslan Ermilov.Fc 58eca67d51SGarrett Wollman.Ft void 5946cdc140SDavid Chisnall.Fo qsort_b 6046cdc140SDavid Chisnall.Fa "void *base" 6146cdc140SDavid Chisnall.Fa "size_t nmemb" 6246cdc140SDavid Chisnall.Fa "size_t size" 6346cdc140SDavid Chisnall.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]" 6446cdc140SDavid Chisnall.Fc 6546cdc140SDavid Chisnall.Ft void 66eca67d51SGarrett Wollman.Fo qsort_r 67eca67d51SGarrett Wollman.Fa "void *base" 68eca67d51SGarrett Wollman.Fa "size_t nmemb" 69eca67d51SGarrett Wollman.Fa "size_t size" 70af3c7888SEd Schouten.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]" 71eca67d51SGarrett Wollman.Fa "void *thunk" 72eca67d51SGarrett Wollman.Fc 7358f0484fSRodney W. Grimes.Ft int 741798791dSRuslan Ermilov.Fo heapsort 751798791dSRuslan Ermilov.Fa "void *base" 761798791dSRuslan Ermilov.Fa "size_t nmemb" 771798791dSRuslan Ermilov.Fa "size_t size" 781798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 791798791dSRuslan Ermilov.Fc 8058f0484fSRodney W. Grimes.Ft int 8146cdc140SDavid Chisnall.Fo heapsort_b 8246cdc140SDavid Chisnall.Fa "void *base" 8346cdc140SDavid Chisnall.Fa "size_t nmemb" 8446cdc140SDavid Chisnall.Fa "size_t size" 8546cdc140SDavid Chisnall.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]" 8646cdc140SDavid Chisnall.Fc 8746cdc140SDavid Chisnall.Ft int 881798791dSRuslan Ermilov.Fo mergesort 891798791dSRuslan Ermilov.Fa "void *base" 901798791dSRuslan Ermilov.Fa "size_t nmemb" 911798791dSRuslan Ermilov.Fa "size_t size" 921798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]" 931798791dSRuslan Ermilov.Fc 9446cdc140SDavid Chisnall.Ft int 9546cdc140SDavid Chisnall.Fo mergesort_b 9646cdc140SDavid Chisnall.Fa "void *base" 9746cdc140SDavid Chisnall.Fa "size_t nmemb" 9846cdc140SDavid Chisnall.Fa "size_t size" 9946cdc140SDavid Chisnall.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]" 10046cdc140SDavid Chisnall.Fc 1010d2fabfcSEdward Tomasz Napierala.Fd #define __STDC_WANT_LIB_EXT1__ 1 1020d2fabfcSEdward Tomasz Napierala.Ft errno_t 1030d2fabfcSEdward Tomasz Napierala.Fo qsort_s 1040d2fabfcSEdward Tomasz Napierala.Fa "void *base" 1050d2fabfcSEdward Tomasz Napierala.Fa "rsize_t nmemb" 1060d2fabfcSEdward Tomasz Napierala.Fa "rsize_t size" 1070d2fabfcSEdward Tomasz Napierala.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]" 1080d2fabfcSEdward Tomasz Napierala.Fa "void *thunk" 1090d2fabfcSEdward Tomasz Napierala.Fc 11058f0484fSRodney W. Grimes.Sh DESCRIPTION 11158f0484fSRodney W. GrimesThe 11258f0484fSRodney W. Grimes.Fn qsort 11358f0484fSRodney W. Grimesfunction is a modified partition-exchange sort, or quicksort. 11458f0484fSRodney W. GrimesThe 11558f0484fSRodney W. Grimes.Fn heapsort 11658f0484fSRodney W. Grimesfunction is a modified selection sort. 11758f0484fSRodney W. GrimesThe 11858f0484fSRodney W. Grimes.Fn mergesort 11958f0484fSRodney W. Grimesfunction is a modified merge sort with exponential search 12058f0484fSRodney W. Grimesintended for sorting data with pre-existing order. 12158f0484fSRodney W. Grimes.Pp 12258f0484fSRodney W. GrimesThe 12358f0484fSRodney W. Grimes.Fn qsort 12458f0484fSRodney W. Grimesand 12558f0484fSRodney W. Grimes.Fn heapsort 12658f0484fSRodney W. Grimesfunctions sort an array of 12758f0484fSRodney W. Grimes.Fa nmemb 12858f0484fSRodney W. Grimesobjects, the initial member of which is pointed to by 12958f0484fSRodney W. Grimes.Fa base . 13058f0484fSRodney W. GrimesThe size of each object is specified by 13158f0484fSRodney W. Grimes.Fa size . 1321fae73b1SRuslan ErmilovThe 1331fae73b1SRuslan Ermilov.Fn mergesort 1341fae73b1SRuslan Ermilovfunction 13558f0484fSRodney W. Grimesbehaves similarly, but 13658f0484fSRodney W. Grimes.Em requires 13758f0484fSRodney W. Grimesthat 13858f0484fSRodney W. Grimes.Fa size 13958f0484fSRodney W. Grimesbe greater than 14058f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" . 14158f0484fSRodney W. Grimes.Pp 14258f0484fSRodney W. GrimesThe contents of the array 14358f0484fSRodney W. Grimes.Fa base 14458f0484fSRodney W. Grimesare sorted in ascending order according to 14558f0484fSRodney W. Grimesa comparison function pointed to by 14658f0484fSRodney W. Grimes.Fa compar , 14758f0484fSRodney W. Grimeswhich requires two arguments pointing to the objects being 14858f0484fSRodney W. Grimescompared. 14958f0484fSRodney W. Grimes.Pp 15058f0484fSRodney W. GrimesThe comparison function must return an integer less than, equal to, or 15158f0484fSRodney W. Grimesgreater than zero if the first argument is considered to be respectively 15258f0484fSRodney W. Grimesless than, equal to, or greater than the second. 15358f0484fSRodney W. Grimes.Pp 154eca67d51SGarrett WollmanThe 155eca67d51SGarrett Wollman.Fn qsort_r 156eca67d51SGarrett Wollmanfunction behaves identically to 157eca67d51SGarrett Wollman.Fn qsort , 158eca67d51SGarrett Wollmanexcept that it takes an additional argument, 159eca67d51SGarrett Wollman.Fa thunk , 160af3c7888SEd Schoutenwhich is passed unchanged as the last argument to function pointed to 161eca67d51SGarrett Wollman.Fa compar . 162eca67d51SGarrett WollmanThis allows the comparison function to access additional 163eca67d51SGarrett Wollmandata without using global variables, and thus 164eca67d51SGarrett Wollman.Fn qsort_r 165eca67d51SGarrett Wollmanis suitable for use in functions which must be reentrant. 16646cdc140SDavid ChisnallThe 16746cdc140SDavid Chisnall.Fn qsort_b 16846cdc140SDavid Chisnallfunction behaves identically to 16946cdc140SDavid Chisnall.Fn qsort , 17046cdc140SDavid Chisnallexcept that it takes a block, rather than a function pointer. 171eca67d51SGarrett Wollman.Pp 172eca67d51SGarrett WollmanThe algorithms implemented by 173eca67d51SGarrett Wollman.Fn qsort , 174eca67d51SGarrett Wollman.Fn qsort_r , 17558f0484fSRodney W. Grimesand 17658f0484fSRodney W. Grimes.Fn heapsort 17758f0484fSRodney W. Grimesare 17858f0484fSRodney W. Grimes.Em not 17958f0484fSRodney W. Grimesstable, that is, if two members compare as equal, their order in 18058f0484fSRodney W. Grimesthe sorted array is undefined. 181eca67d51SGarrett WollmanThe 18246cdc140SDavid Chisnall.Fn heapsort_b 18346cdc140SDavid Chisnallfunction behaves identically to 18446cdc140SDavid Chisnall.Fn heapsort , 18546cdc140SDavid Chisnallexcept that it takes a block, rather than a function pointer. 18646cdc140SDavid ChisnallThe 18758f0484fSRodney W. Grimes.Fn mergesort 188eca67d51SGarrett Wollmanalgorithm is stable. 18946cdc140SDavid ChisnallThe 19046cdc140SDavid Chisnall.Fn mergesort_b 19146cdc140SDavid Chisnallfunction behaves identically to 19246cdc140SDavid Chisnall.Fn mergesort , 19346cdc140SDavid Chisnallexcept that it takes a block, rather than a function pointer. 19458f0484fSRodney W. Grimes.Pp 19558f0484fSRodney W. GrimesThe 19658f0484fSRodney W. Grimes.Fn qsort 197eca67d51SGarrett Wollmanand 198eca67d51SGarrett Wollman.Fn qsort_r 1991a0a9345SRuslan Ermilovfunctions are an implementation of C.A.R. 2001a0a9345SRuslan ErmilovHoare's 2011798791dSRuslan Ermilov.Dq quicksort 2021798791dSRuslan Ermilovalgorithm, 2031a0a9345SRuslan Ermilova variant of partition-exchange sorting; in particular, see 2041a0a9345SRuslan Ermilov.An D.E. Knuth Ns 's 2051a0a9345SRuslan Ermilov.%T "Algorithm Q" . 206eca67d51SGarrett Wollman.Sy Quicksort 20758f0484fSRodney W. Grimestakes O N lg N average time. 20858f0484fSRodney W. GrimesThis implementation uses median selection to avoid its 20958f0484fSRodney W. GrimesO N**2 worst-case behavior. 21058f0484fSRodney W. Grimes.Pp 21158f0484fSRodney W. GrimesThe 21258f0484fSRodney W. Grimes.Fn heapsort 2131a0a9345SRuslan Ermilovfunction is an implementation of 2141a0a9345SRuslan Ermilov.An "J.W.J. William" Ns 's 2151798791dSRuslan Ermilov.Dq heapsort 2161798791dSRuslan Ermilovalgorithm, 2171a0a9345SRuslan Ermilova variant of selection sorting; in particular, see 2181a0a9345SRuslan Ermilov.An "D.E. Knuth" Ns 's 2191a0a9345SRuslan Ermilov.%T "Algorithm H" . 220eca67d51SGarrett Wollman.Sy Heapsort 22158f0484fSRodney W. Grimestakes O N lg N worst-case time. 22258f0484fSRodney W. GrimesIts 22358f0484fSRodney W. Grimes.Em only 22458f0484fSRodney W. Grimesadvantage over 22558f0484fSRodney W. Grimes.Fn qsort 22658f0484fSRodney W. Grimesis that it uses almost no additional memory; while 22758f0484fSRodney W. Grimes.Fn qsort 22858f0484fSRodney W. Grimesdoes not allocate memory, it is implemented using recursion. 22958f0484fSRodney W. Grimes.Pp 23058f0484fSRodney W. GrimesThe function 23158f0484fSRodney W. Grimes.Fn mergesort 23258f0484fSRodney W. Grimesrequires additional memory of size 23358f0484fSRodney W. Grimes.Fa nmemb * 23458f0484fSRodney W. Grimes.Fa size 23558f0484fSRodney W. Grimesbytes; it should be used only when space is not at a premium. 2361fae73b1SRuslan ErmilovThe 2371fae73b1SRuslan Ermilov.Fn mergesort 2381fae73b1SRuslan Ermilovfunction 23958f0484fSRodney W. Grimesis optimized for data with pre-existing order; its worst case 24058f0484fSRodney W. Grimestime is O N lg N; its best case is O N. 24158f0484fSRodney W. Grimes.Pp 24258f0484fSRodney W. GrimesNormally, 24358f0484fSRodney W. Grimes.Fn qsort 24458f0484fSRodney W. Grimesis faster than 24558f0484fSRodney W. Grimes.Fn mergesort 24658f0484fSRodney W. Grimesis faster than 24758f0484fSRodney W. Grimes.Fn heapsort . 24858f0484fSRodney W. GrimesMemory availability and pre-existing order in the data can make this 24958f0484fSRodney W. Grimesuntrue. 2500d2fabfcSEdward Tomasz Napierala.Pp 2510d2fabfcSEdward Tomasz NapieralaThe 2520d2fabfcSEdward Tomasz Napierala.Fn qsort_s 2530d2fabfcSEdward Tomasz Napieralafunction behaves the same as 2540d2fabfcSEdward Tomasz Napierala.Fn qsort_r , except that: 2550d2fabfcSEdward Tomasz Napierala.Bl -dash 2560d2fabfcSEdward Tomasz Napierala.It 2570d2fabfcSEdward Tomasz NapieralaThe order of arguments is different 2580d2fabfcSEdward Tomasz Napierala.It 2590d2fabfcSEdward Tomasz NapieralaThe order of arguments to 2600d2fabfcSEdward Tomasz Napierala.Fa compar 2610d2fabfcSEdward Tomasz Napieralais different 2620d2fabfcSEdward Tomasz Napierala.It 263*27bb0d33SHans Petter SelaskyIf 2640d2fabfcSEdward Tomasz Napierala.Fa nmemb 2650d2fabfcSEdward Tomasz Napieralaor 2660d2fabfcSEdward Tomasz Napierala.Fa size 2670d2fabfcSEdward Tomasz Napieralaare greater than 2680d2fabfcSEdward Tomasz Napierala.Dv RSIZE_MAX , 2690d2fabfcSEdward Tomasz Napieralaor 2700d2fabfcSEdward Tomasz Napierala.Fa nmemb 2710d2fabfcSEdward Tomasz Napieralais not zero and 2720d2fabfcSEdward Tomasz Napierala.Fa compar 273*27bb0d33SHans Petter Selaskyis 274*27bb0d33SHans Petter Selasky.Dv NULL 275*27bb0d33SHans Petter Selaskyor 276*27bb0d33SHans Petter Selasky.Fa size 277*27bb0d33SHans Petter Selaskyis zero, then the runtime-constraint handler is called, and 2780d2fabfcSEdward Tomasz Napierala.Fn qsort_s 2790d2fabfcSEdward Tomasz Napieralareturns an error. 2800d2fabfcSEdward Tomasz NapieralaNote that the handler is called before 2810d2fabfcSEdward Tomasz Napierala.Fn qsort_s 2820d2fabfcSEdward Tomasz Napieralareturns the error, and the handler function might not return. 2830d2fabfcSEdward Tomasz Napierala.El 28458f0484fSRodney W. Grimes.Sh RETURN VALUES 28558f0484fSRodney W. GrimesThe 28658f0484fSRodney W. Grimes.Fn qsort 287eca67d51SGarrett Wollmanand 288eca67d51SGarrett Wollman.Fn qsort_r 289eca67d51SGarrett Wollmanfunctions 290eca67d51SGarrett Wollmanreturn no value. 2910d2fabfcSEdward Tomasz NapieralaThe 2920d2fabfcSEdward Tomasz Napierala.Fn qsort_s 2930d2fabfcSEdward Tomasz Napieralafunction returns zero on success, non-zero on error. 29458f0484fSRodney W. Grimes.Pp 295d6002fefSRuslan Ermilov.Rv -std heapsort mergesort 2968ce3e01eSGiorgos Keramidas.Sh EXAMPLES 2978ce3e01eSGiorgos KeramidasA sample program that sorts an array of 2988ce3e01eSGiorgos Keramidas.Vt int 2998ce3e01eSGiorgos Keramidasvalues in place using 3008ce3e01eSGiorgos Keramidas.Fn qsort , 3018ce3e01eSGiorgos Keramidasand then prints the sorted array to standard output is: 3028ce3e01eSGiorgos Keramidas.Bd -literal 3038ce3e01eSGiorgos Keramidas#include <stdio.h> 3048ce3e01eSGiorgos Keramidas#include <stdlib.h> 3058ce3e01eSGiorgos Keramidas 3068ce3e01eSGiorgos Keramidas/* 30748ac3a2aSSergey Kandaurov * Custom comparison function that compares 'int' values through pointers 3088ce3e01eSGiorgos Keramidas * passed by qsort(3). 3098ce3e01eSGiorgos Keramidas */ 3108ce3e01eSGiorgos Keramidasstatic int 3118ce3e01eSGiorgos Keramidasint_compare(const void *p1, const void *p2) 3128ce3e01eSGiorgos Keramidas{ 313302318d5SGiorgos Keramidas int left = *(const int *)p1; 314302318d5SGiorgos Keramidas int right = *(const int *)p2; 3158ce3e01eSGiorgos Keramidas 316302318d5SGiorgos Keramidas return ((left > right) - (left < right)); 3178ce3e01eSGiorgos Keramidas} 3188ce3e01eSGiorgos Keramidas 3198ce3e01eSGiorgos Keramidas/* 3208ce3e01eSGiorgos Keramidas * Sort an array of 'int' values and print it to standard output. 3218ce3e01eSGiorgos Keramidas */ 3228ce3e01eSGiorgos Keramidasint 3238ce3e01eSGiorgos Keramidasmain(void) 3248ce3e01eSGiorgos Keramidas{ 3258ce3e01eSGiorgos Keramidas int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 }; 32648ac3a2aSSergey Kandaurov size_t array_size = sizeof(int_array) / sizeof(int_array[0]); 327302318d5SGiorgos Keramidas size_t k; 3288ce3e01eSGiorgos Keramidas 329302318d5SGiorgos Keramidas qsort(&int_array, array_size, sizeof(int_array[0]), int_compare); 3308ce3e01eSGiorgos Keramidas for (k = 0; k < array_size; k++) 3318ce3e01eSGiorgos Keramidas printf(" %d", int_array[k]); 332302318d5SGiorgos Keramidas puts(""); 333302318d5SGiorgos Keramidas return (EXIT_SUCCESS); 3348ce3e01eSGiorgos Keramidas} 3358ce3e01eSGiorgos Keramidas.Ed 336954349a6SJoel Dahl.Sh COMPATIBILITY 3370d2fabfcSEdward Tomasz NapieralaThe order of arguments for the comparison function used with 3380d2fabfcSEdward Tomasz Napierala.Fn qsort_r 3390d2fabfcSEdward Tomasz Napieralais different from the one used by 3400d2fabfcSEdward Tomasz Napierala.Fn qsort_s , 3410d2fabfcSEdward Tomasz Napieralaand the GNU libc implementation of 3420d2fabfcSEdward Tomasz Napierala.Fn qsort_r . 3430d2fabfcSEdward Tomasz NapieralaWhen porting software written for GNU libc, it is usually possible 3440d2fabfcSEdward Tomasz Napieralato replace 3450d2fabfcSEdward Tomasz Napierala.Fn qsort_r 3460d2fabfcSEdward Tomasz Napieralawith 3470d2fabfcSEdward Tomasz Napierala.Fn qsort_s 3480d2fabfcSEdward Tomasz Napieralato work around this problem. 3490d2fabfcSEdward Tomasz Napierala.Pp 350ae39ed86SConrad Meyer.Fn qsort_s 351ae39ed86SConrad Meyeris part of the 352ae39ed86SConrad Meyer.Em optional 353ae39ed86SConrad MeyerAnnex K portion of 354ae39ed86SConrad Meyer.St -isoC-2011 355ae39ed86SConrad Meyerand may not be portable to other standards-conforming platforms. 356ae39ed86SConrad Meyer.Pp 357954349a6SJoel DahlPrevious versions of 358954349a6SJoel Dahl.Fn qsort 359954349a6SJoel Dahldid not permit the comparison routine itself to call 360954349a6SJoel Dahl.Fn qsort 3 . 361954349a6SJoel DahlThis is no longer true. 36258f0484fSRodney W. Grimes.Sh ERRORS 36358f0484fSRodney W. GrimesThe 36458f0484fSRodney W. Grimes.Fn heapsort 3658d2c3c32SRobert Nordierand 3668d2c3c32SRobert Nordier.Fn mergesort 3678d2c3c32SRobert Nordierfunctions succeed unless: 36858f0484fSRodney W. Grimes.Bl -tag -width Er 36958f0484fSRodney W. Grimes.It Bq Er EINVAL 37058f0484fSRodney W. GrimesThe 37158f0484fSRodney W. Grimes.Fa size 37258f0484fSRodney W. Grimesargument is zero, or, 37358f0484fSRodney W. Grimesthe 37458f0484fSRodney W. Grimes.Fa size 37558f0484fSRodney W. Grimesargument to 37658f0484fSRodney W. Grimes.Fn mergesort 37758f0484fSRodney W. Grimesis less than 37858f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" . 37958f0484fSRodney W. Grimes.It Bq Er ENOMEM 3801fae73b1SRuslan ErmilovThe 3811fae73b1SRuslan Ermilov.Fn heapsort 38258f0484fSRodney W. Grimesor 38358f0484fSRodney W. Grimes.Fn mergesort 3841fae73b1SRuslan Ermilovfunctions 38558f0484fSRodney W. Grimeswere unable to allocate memory. 38658f0484fSRodney W. Grimes.El 38758f0484fSRodney W. Grimes.Sh SEE ALSO 38858f0484fSRodney W. Grimes.Xr sort 1 , 38958f0484fSRodney W. Grimes.Xr radixsort 3 39058f0484fSRodney W. Grimes.Rs 39158f0484fSRodney W. Grimes.%A Hoare, C.A.R. 39258f0484fSRodney W. Grimes.%D 1962 39358f0484fSRodney W. Grimes.%T "Quicksort" 39458f0484fSRodney W. Grimes.%J "The Computer Journal" 39558f0484fSRodney W. Grimes.%V 5:1 39658f0484fSRodney W. Grimes.%P pp. 10-15 39758f0484fSRodney W. Grimes.Re 39858f0484fSRodney W. Grimes.Rs 39958f0484fSRodney W. Grimes.%A Williams, J.W.J 40058f0484fSRodney W. Grimes.%D 1964 40158f0484fSRodney W. Grimes.%T "Heapsort" 40258f0484fSRodney W. Grimes.%J "Communications of the ACM" 40358f0484fSRodney W. Grimes.%V 7:1 40458f0484fSRodney W. Grimes.%P pp. 347-348 40558f0484fSRodney W. Grimes.Re 40658f0484fSRodney W. Grimes.Rs 40758f0484fSRodney W. Grimes.%A Knuth, D.E. 40858f0484fSRodney W. Grimes.%D 1968 40958f0484fSRodney W. Grimes.%B "The Art of Computer Programming" 41058f0484fSRodney W. Grimes.%V Vol. 3 41158f0484fSRodney W. Grimes.%T "Sorting and Searching" 41258f0484fSRodney W. Grimes.%P pp. 114-123, 145-149 41358f0484fSRodney W. Grimes.Re 41458f0484fSRodney W. Grimes.Rs 4155e24a424STim J. Robbins.%A McIlroy, P.M. 41658f0484fSRodney W. Grimes.%T "Optimistic Sorting and Information Theoretic Complexity" 41758f0484fSRodney W. Grimes.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" 41858f0484fSRodney W. Grimes.%V January 1992 41958f0484fSRodney W. Grimes.Re 42058f0484fSRodney W. Grimes.Rs 42158f0484fSRodney W. Grimes.%A Bentley, J.L. 4225e24a424STim J. Robbins.%A McIlroy, M.D. 42358f0484fSRodney W. Grimes.%T "Engineering a Sort Function" 4245e24a424STim J. Robbins.%J "Software--Practice and Experience" 4255e24a424STim J. Robbins.%V Vol. 23(11) 4265e24a424STim J. Robbins.%P pp. 1249-1265 4275e24a424STim J. Robbins.%D November\ 1993 42858f0484fSRodney W. Grimes.Re 42958f0484fSRodney W. Grimes.Sh STANDARDS 43058f0484fSRodney W. GrimesThe 43158f0484fSRodney W. Grimes.Fn qsort 43258f0484fSRodney W. Grimesfunction 43358f0484fSRodney W. Grimesconforms to 434588a200cSRuslan Ermilov.St -isoC . 4350d2fabfcSEdward Tomasz Napierala.Fn qsort_s 4360d2fabfcSEdward Tomasz Napieralaconforms to 4370d2fabfcSEdward Tomasz Napierala.St -isoC-2011 4380d2fabfcSEdward Tomasz NapieralaK.3.6.3.2. 43946cdc140SDavid Chisnall.Sh HISTORY 44046cdc140SDavid ChisnallThe variants of these functions that take blocks as arguments first appeared in 44146cdc140SDavid ChisnallMac OS X. 44246cdc140SDavid ChisnallThis implementation was created by David Chisnall. 443af3c7888SEd Schouten.Pp 444af3c7888SEd SchoutenIn 445af3c7888SEd Schouten.Fx 14.0 , 446af3c7888SEd Schoutenthe prototype of 447af3c7888SEd Schouten.Fn qsort_r 448af3c7888SEd Schoutenwas updated to match POSIX. 449