xref: /freebsd/lib/libc/stdlib/qsort.3 (revision 3df1abdfd9c309efbdc7884d6b6f6fe25efcb397)
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.\"
32*3df1abdfSXin LI.Dd October 25, 2024
3358f0484fSRodney W. Grimes.Dt QSORT 3
3458f0484fSRodney W. Grimes.Os
3558f0484fSRodney W. Grimes.Sh NAME
36604f1c41SEdward Tomasz Napierala.Nm qsort ,
37604f1c41SEdward Tomasz Napierala.Nm qsort_b ,
38604f1c41SEdward Tomasz Napierala.Nm qsort_r ,
39604f1c41SEdward Tomasz Napierala.Nm heapsort ,
40604f1c41SEdward Tomasz Napierala.Nm heapsort_b ,
41604f1c41SEdward Tomasz Napierala.Nm mergesort ,
42604f1c41SEdward Tomasz Napierala.Nm mergesort_b
4358f0484fSRodney W. Grimes.Nd sort functions
4425bb73e0SAlexey Zelkin.Sh LIBRARY
4525bb73e0SAlexey Zelkin.Lb libc
4658f0484fSRodney W. Grimes.Sh SYNOPSIS
478aefde06SJeroen Ruigrok van der Werven.In stdlib.h
4858f0484fSRodney W. Grimes.Ft void
491798791dSRuslan Ermilov.Fo qsort
501798791dSRuslan Ermilov.Fa "void *base"
511798791dSRuslan Ermilov.Fa "size_t nmemb"
521798791dSRuslan Ermilov.Fa "size_t size"
531798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
541798791dSRuslan Ermilov.Fc
55eca67d51SGarrett Wollman.Ft void
5646cdc140SDavid Chisnall.Fo qsort_b
5746cdc140SDavid Chisnall.Fa "void *base"
5846cdc140SDavid Chisnall.Fa "size_t nmemb"
5946cdc140SDavid Chisnall.Fa "size_t size"
6046cdc140SDavid Chisnall.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
6146cdc140SDavid Chisnall.Fc
6246cdc140SDavid Chisnall.Ft void
63eca67d51SGarrett Wollman.Fo qsort_r
64eca67d51SGarrett Wollman.Fa "void *base"
65eca67d51SGarrett Wollman.Fa "size_t nmemb"
66eca67d51SGarrett Wollman.Fa "size_t size"
67af3c7888SEd Schouten.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]"
68eca67d51SGarrett Wollman.Fa "void *thunk"
69eca67d51SGarrett Wollman.Fc
7058f0484fSRodney W. Grimes.Ft int
711798791dSRuslan Ermilov.Fo heapsort
721798791dSRuslan Ermilov.Fa "void *base"
731798791dSRuslan Ermilov.Fa "size_t nmemb"
741798791dSRuslan Ermilov.Fa "size_t size"
751798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
761798791dSRuslan Ermilov.Fc
7758f0484fSRodney W. Grimes.Ft int
7846cdc140SDavid Chisnall.Fo heapsort_b
7946cdc140SDavid Chisnall.Fa "void *base"
8046cdc140SDavid Chisnall.Fa "size_t nmemb"
8146cdc140SDavid Chisnall.Fa "size_t size"
8246cdc140SDavid Chisnall.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
8346cdc140SDavid Chisnall.Fc
8446cdc140SDavid Chisnall.Ft int
851798791dSRuslan Ermilov.Fo mergesort
861798791dSRuslan Ermilov.Fa "void *base"
871798791dSRuslan Ermilov.Fa "size_t nmemb"
881798791dSRuslan Ermilov.Fa "size_t size"
891798791dSRuslan Ermilov.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *\*[rp]"
901798791dSRuslan Ermilov.Fc
9146cdc140SDavid Chisnall.Ft int
9246cdc140SDavid Chisnall.Fo mergesort_b
9346cdc140SDavid Chisnall.Fa "void *base"
9446cdc140SDavid Chisnall.Fa "size_t nmemb"
9546cdc140SDavid Chisnall.Fa "size_t size"
9646cdc140SDavid Chisnall.Fa "int \*[lp]^compar\*[rp]\*[lp]const void *, const void *\*[rp]"
9746cdc140SDavid Chisnall.Fc
980d2fabfcSEdward Tomasz Napierala.Fd #define __STDC_WANT_LIB_EXT1__ 1
990d2fabfcSEdward Tomasz Napierala.Ft errno_t
1000d2fabfcSEdward Tomasz Napierala.Fo qsort_s
1010d2fabfcSEdward Tomasz Napierala.Fa "void *base"
1020d2fabfcSEdward Tomasz Napierala.Fa "rsize_t nmemb"
1030d2fabfcSEdward Tomasz Napierala.Fa "rsize_t size"
1040d2fabfcSEdward Tomasz Napierala.Fa "int \*[lp]*compar\*[rp]\*[lp]const void *, const void *, void *\*[rp]"
1050d2fabfcSEdward Tomasz Napierala.Fa "void *thunk"
1060d2fabfcSEdward Tomasz Napierala.Fc
10758f0484fSRodney W. Grimes.Sh DESCRIPTION
10858f0484fSRodney W. GrimesThe
10958f0484fSRodney W. Grimes.Fn qsort
11058f0484fSRodney W. Grimesfunction is a modified partition-exchange sort, or quicksort.
11158f0484fSRodney W. GrimesThe
11258f0484fSRodney W. Grimes.Fn heapsort
11358f0484fSRodney W. Grimesfunction is a modified selection sort.
11458f0484fSRodney W. GrimesThe
11558f0484fSRodney W. Grimes.Fn mergesort
11658f0484fSRodney W. Grimesfunction is a modified merge sort with exponential search
11758f0484fSRodney W. Grimesintended for sorting data with pre-existing order.
11858f0484fSRodney W. Grimes.Pp
11958f0484fSRodney W. GrimesThe
12058f0484fSRodney W. Grimes.Fn qsort
12158f0484fSRodney W. Grimesand
12258f0484fSRodney W. Grimes.Fn heapsort
12358f0484fSRodney W. Grimesfunctions sort an array of
12458f0484fSRodney W. Grimes.Fa nmemb
12558f0484fSRodney W. Grimesobjects, the initial member of which is pointed to by
12658f0484fSRodney W. Grimes.Fa base .
12758f0484fSRodney W. GrimesThe size of each object is specified by
12858f0484fSRodney W. Grimes.Fa size .
1291fae73b1SRuslan ErmilovThe
1301fae73b1SRuslan Ermilov.Fn mergesort
1311fae73b1SRuslan Ermilovfunction
13258f0484fSRodney W. Grimesbehaves similarly, but
13358f0484fSRodney W. Grimes.Em requires
13458f0484fSRodney W. Grimesthat
13558f0484fSRodney W. Grimes.Fa size
13658f0484fSRodney W. Grimesbe greater than
13758f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" .
13858f0484fSRodney W. Grimes.Pp
13958f0484fSRodney W. GrimesThe contents of the array
14058f0484fSRodney W. Grimes.Fa base
14158f0484fSRodney W. Grimesare sorted in ascending order according to
14258f0484fSRodney W. Grimesa comparison function pointed to by
14358f0484fSRodney W. Grimes.Fa compar ,
14458f0484fSRodney W. Grimeswhich requires two arguments pointing to the objects being
14558f0484fSRodney W. Grimescompared.
14658f0484fSRodney W. Grimes.Pp
14758f0484fSRodney W. GrimesThe comparison function must return an integer less than, equal to, or
14858f0484fSRodney W. Grimesgreater than zero if the first argument is considered to be respectively
14958f0484fSRodney W. Grimesless than, equal to, or greater than the second.
15058f0484fSRodney W. Grimes.Pp
151eca67d51SGarrett WollmanThe
152eca67d51SGarrett Wollman.Fn qsort_r
153eca67d51SGarrett Wollmanfunction behaves identically to
154eca67d51SGarrett Wollman.Fn qsort ,
155eca67d51SGarrett Wollmanexcept that it takes an additional argument,
156eca67d51SGarrett Wollman.Fa thunk ,
157af3c7888SEd Schoutenwhich is passed unchanged as the last argument to function pointed to
158eca67d51SGarrett Wollman.Fa compar .
159eca67d51SGarrett WollmanThis allows the comparison function to access additional
160eca67d51SGarrett Wollmandata without using global variables, and thus
161eca67d51SGarrett Wollman.Fn qsort_r
162eca67d51SGarrett Wollmanis suitable for use in functions which must be reentrant.
16346cdc140SDavid ChisnallThe
16446cdc140SDavid Chisnall.Fn qsort_b
16546cdc140SDavid Chisnallfunction behaves identically to
16646cdc140SDavid Chisnall.Fn qsort ,
16746cdc140SDavid Chisnallexcept that it takes a block, rather than a function pointer.
168eca67d51SGarrett Wollman.Pp
169eca67d51SGarrett WollmanThe algorithms implemented by
170eca67d51SGarrett Wollman.Fn qsort ,
171eca67d51SGarrett Wollman.Fn qsort_r ,
17258f0484fSRodney W. Grimesand
17358f0484fSRodney W. Grimes.Fn heapsort
17458f0484fSRodney W. Grimesare
17558f0484fSRodney W. Grimes.Em not
17658f0484fSRodney W. Grimesstable, that is, if two members compare as equal, their order in
17758f0484fSRodney W. Grimesthe sorted array is undefined.
178eca67d51SGarrett WollmanThe
17946cdc140SDavid Chisnall.Fn heapsort_b
18046cdc140SDavid Chisnallfunction behaves identically to
18146cdc140SDavid Chisnall.Fn heapsort ,
18246cdc140SDavid Chisnallexcept that it takes a block, rather than a function pointer.
18346cdc140SDavid ChisnallThe
18458f0484fSRodney W. Grimes.Fn mergesort
185eca67d51SGarrett Wollmanalgorithm is stable.
18646cdc140SDavid ChisnallThe
18746cdc140SDavid Chisnall.Fn mergesort_b
18846cdc140SDavid Chisnallfunction behaves identically to
18946cdc140SDavid Chisnall.Fn mergesort ,
19046cdc140SDavid Chisnallexcept that it takes a block, rather than a function pointer.
19158f0484fSRodney W. Grimes.Pp
19258f0484fSRodney W. GrimesThe
19358f0484fSRodney W. Grimes.Fn qsort
194eca67d51SGarrett Wollmanand
195eca67d51SGarrett Wollman.Fn qsort_r
1961a0a9345SRuslan Ermilovfunctions are an implementation of C.A.R.
1971a0a9345SRuslan ErmilovHoare's
1981798791dSRuslan Ermilov.Dq quicksort
1991798791dSRuslan Ermilovalgorithm,
2001a0a9345SRuslan Ermilova variant of partition-exchange sorting; in particular, see
2011a0a9345SRuslan Ermilov.An D.E. Knuth Ns 's
2021a0a9345SRuslan Ermilov.%T "Algorithm Q" .
203eca67d51SGarrett Wollman.Sy Quicksort
20458f0484fSRodney W. Grimestakes O N lg N average time.
20558f0484fSRodney W. GrimesThis implementation uses median selection to avoid its
20658f0484fSRodney W. GrimesO N**2 worst-case behavior.
20758f0484fSRodney W. Grimes.Pp
20858f0484fSRodney W. GrimesThe
20958f0484fSRodney W. Grimes.Fn heapsort
2101a0a9345SRuslan Ermilovfunction is an implementation of
2111a0a9345SRuslan Ermilov.An "J.W.J. William" Ns 's
2121798791dSRuslan Ermilov.Dq heapsort
2131798791dSRuslan Ermilovalgorithm,
2141a0a9345SRuslan Ermilova variant of selection sorting; in particular, see
2151a0a9345SRuslan Ermilov.An "D.E. Knuth" Ns 's
2161a0a9345SRuslan Ermilov.%T "Algorithm H" .
217eca67d51SGarrett Wollman.Sy Heapsort
21858f0484fSRodney W. Grimestakes O N lg N worst-case time.
21958f0484fSRodney W. GrimesIts
22058f0484fSRodney W. Grimes.Em only
22158f0484fSRodney W. Grimesadvantage over
22258f0484fSRodney W. Grimes.Fn qsort
22358f0484fSRodney W. Grimesis that it uses almost no additional memory; while
22458f0484fSRodney W. Grimes.Fn qsort
22558f0484fSRodney W. Grimesdoes not allocate memory, it is implemented using recursion.
22658f0484fSRodney W. Grimes.Pp
22758f0484fSRodney W. GrimesThe function
22858f0484fSRodney W. Grimes.Fn mergesort
22958f0484fSRodney W. Grimesrequires additional memory of size
23058f0484fSRodney W. Grimes.Fa nmemb *
23158f0484fSRodney W. Grimes.Fa size
23258f0484fSRodney W. Grimesbytes; it should be used only when space is not at a premium.
2331fae73b1SRuslan ErmilovThe
2341fae73b1SRuslan Ermilov.Fn mergesort
2351fae73b1SRuslan Ermilovfunction
23658f0484fSRodney W. Grimesis optimized for data with pre-existing order; its worst case
23758f0484fSRodney W. Grimestime is O N lg N; its best case is O N.
23858f0484fSRodney W. Grimes.Pp
23958f0484fSRodney W. GrimesNormally,
24058f0484fSRodney W. Grimes.Fn qsort
24158f0484fSRodney W. Grimesis faster than
24258f0484fSRodney W. Grimes.Fn mergesort
24358f0484fSRodney W. Grimesis faster than
24458f0484fSRodney W. Grimes.Fn heapsort .
24558f0484fSRodney W. GrimesMemory availability and pre-existing order in the data can make this
24658f0484fSRodney W. Grimesuntrue.
2470d2fabfcSEdward Tomasz Napierala.Pp
2480d2fabfcSEdward Tomasz NapieralaThe
2490d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2500d2fabfcSEdward Tomasz Napieralafunction behaves the same as
251*3df1abdfSXin LI.Fn qsort_r ,
252*3df1abdfSXin LIexcept that if
2530d2fabfcSEdward Tomasz Napierala.Fa nmemb
2540d2fabfcSEdward Tomasz Napieralaor
2550d2fabfcSEdward Tomasz Napierala.Fa size
2560d2fabfcSEdward Tomasz Napieralaare greater than
2570d2fabfcSEdward Tomasz Napierala.Dv RSIZE_MAX ,
2580d2fabfcSEdward Tomasz Napieralaor
2590d2fabfcSEdward Tomasz Napierala.Fa nmemb
2600d2fabfcSEdward Tomasz Napieralais not zero and
2610d2fabfcSEdward Tomasz Napierala.Fa compar
26227bb0d33SHans Petter Selaskyis
26327bb0d33SHans Petter Selasky.Dv NULL
26427bb0d33SHans Petter Selaskyor
26527bb0d33SHans Petter Selasky.Fa size
26627bb0d33SHans Petter Selaskyis zero, then the runtime-constraint handler is called, and
2670d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2680d2fabfcSEdward Tomasz Napieralareturns an error.
2690d2fabfcSEdward Tomasz NapieralaNote that the handler is called before
2700d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2710d2fabfcSEdward Tomasz Napieralareturns the error, and the handler function might not return.
27258f0484fSRodney W. Grimes.Sh RETURN VALUES
27358f0484fSRodney W. GrimesThe
27458f0484fSRodney W. Grimes.Fn qsort
275eca67d51SGarrett Wollmanand
276eca67d51SGarrett Wollman.Fn qsort_r
277eca67d51SGarrett Wollmanfunctions
278eca67d51SGarrett Wollmanreturn no value.
2790d2fabfcSEdward Tomasz NapieralaThe
2800d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2810d2fabfcSEdward Tomasz Napieralafunction returns zero on success, non-zero on error.
28258f0484fSRodney W. Grimes.Pp
283d6002fefSRuslan Ermilov.Rv -std heapsort mergesort
2848ce3e01eSGiorgos Keramidas.Sh EXAMPLES
2858ce3e01eSGiorgos KeramidasA sample program that sorts an array of
2868ce3e01eSGiorgos Keramidas.Vt int
2878ce3e01eSGiorgos Keramidasvalues in place using
2888ce3e01eSGiorgos Keramidas.Fn qsort ,
2898ce3e01eSGiorgos Keramidasand then prints the sorted array to standard output is:
2908ce3e01eSGiorgos Keramidas.Bd -literal
2918ce3e01eSGiorgos Keramidas#include <stdio.h>
2928ce3e01eSGiorgos Keramidas#include <stdlib.h>
2938ce3e01eSGiorgos Keramidas
2948ce3e01eSGiorgos Keramidas/*
29548ac3a2aSSergey Kandaurov * Custom comparison function that compares 'int' values through pointers
2968ce3e01eSGiorgos Keramidas * passed by qsort(3).
2978ce3e01eSGiorgos Keramidas */
2988ce3e01eSGiorgos Keramidasstatic int
2998ce3e01eSGiorgos Keramidasint_compare(const void *p1, const void *p2)
3008ce3e01eSGiorgos Keramidas{
301302318d5SGiorgos Keramidas	int left = *(const int *)p1;
302302318d5SGiorgos Keramidas	int right = *(const int *)p2;
3038ce3e01eSGiorgos Keramidas
304302318d5SGiorgos Keramidas	return ((left > right) - (left < right));
3058ce3e01eSGiorgos Keramidas}
3068ce3e01eSGiorgos Keramidas
3078ce3e01eSGiorgos Keramidas/*
3088ce3e01eSGiorgos Keramidas * Sort an array of 'int' values and print it to standard output.
3098ce3e01eSGiorgos Keramidas */
3108ce3e01eSGiorgos Keramidasint
3118ce3e01eSGiorgos Keramidasmain(void)
3128ce3e01eSGiorgos Keramidas{
3138ce3e01eSGiorgos Keramidas	int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 };
31448ac3a2aSSergey Kandaurov	size_t array_size = sizeof(int_array) / sizeof(int_array[0]);
315302318d5SGiorgos Keramidas	size_t k;
3168ce3e01eSGiorgos Keramidas
317302318d5SGiorgos Keramidas	qsort(&int_array, array_size, sizeof(int_array[0]), int_compare);
3188ce3e01eSGiorgos Keramidas	for (k = 0; k < array_size; k++)
3198ce3e01eSGiorgos Keramidas		printf(" %d", int_array[k]);
320302318d5SGiorgos Keramidas	puts("");
321302318d5SGiorgos Keramidas	return (EXIT_SUCCESS);
3228ce3e01eSGiorgos Keramidas}
3238ce3e01eSGiorgos Keramidas.Ed
324954349a6SJoel Dahl.Sh COMPATIBILITY
3250d2fabfcSEdward Tomasz NapieralaThe order of arguments for the comparison function used with
3260d2fabfcSEdward Tomasz Napierala.Fn qsort_r
327*3df1abdfSXin LIis historically different from the one used by
328*3df1abdfSXin LI.Fn qsort_s
3290d2fabfcSEdward Tomasz Napieralaand the GNU libc implementation of
3300d2fabfcSEdward Tomasz Napierala.Fn qsort_r .
331*3df1abdfSXin LIHowever, as of
332*3df1abdfSXin LI.Fx 14.0 ,
333*3df1abdfSXin LIthe
3340d2fabfcSEdward Tomasz Napierala.Fn qsort_r
335*3df1abdfSXin LIhas been updated so that the
336*3df1abdfSXin LI.Fa thunk
337*3df1abdfSXin LIparameter appears last to match
338*3df1abdfSXin LI.St -p1003.1-2024 .
339*3df1abdfSXin LIBoth the historical and the updated interfaces are now accepted
340*3df1abdfSXin LIvia C11 generic selection and C++ polymorphism,
341*3df1abdfSXin LIbut the updated interface is recommended for portable applications.
3420d2fabfcSEdward Tomasz Napierala.Pp
343ae39ed86SConrad Meyer.Fn qsort_s
344ae39ed86SConrad Meyeris part of the
345ae39ed86SConrad Meyer.Em optional
346ae39ed86SConrad MeyerAnnex K portion of
347ae39ed86SConrad Meyer.St -isoC-2011
348ae39ed86SConrad Meyerand may not be portable to other standards-conforming platforms.
349ae39ed86SConrad Meyer.Pp
350954349a6SJoel DahlPrevious versions of
351954349a6SJoel Dahl.Fn qsort
352954349a6SJoel Dahldid not permit the comparison routine itself to call
353954349a6SJoel Dahl.Fn qsort 3 .
354954349a6SJoel DahlThis is no longer true.
35558f0484fSRodney W. Grimes.Sh ERRORS
35658f0484fSRodney W. GrimesThe
35758f0484fSRodney W. Grimes.Fn heapsort
3588d2c3c32SRobert Nordierand
3598d2c3c32SRobert Nordier.Fn mergesort
3608d2c3c32SRobert Nordierfunctions succeed unless:
36158f0484fSRodney W. Grimes.Bl -tag -width Er
36258f0484fSRodney W. Grimes.It Bq Er EINVAL
36358f0484fSRodney W. GrimesThe
36458f0484fSRodney W. Grimes.Fa size
36558f0484fSRodney W. Grimesargument is zero, or,
36658f0484fSRodney W. Grimesthe
36758f0484fSRodney W. Grimes.Fa size
36858f0484fSRodney W. Grimesargument to
36958f0484fSRodney W. Grimes.Fn mergesort
37058f0484fSRodney W. Grimesis less than
37158f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" .
37258f0484fSRodney W. Grimes.It Bq Er ENOMEM
3731fae73b1SRuslan ErmilovThe
3741fae73b1SRuslan Ermilov.Fn heapsort
37558f0484fSRodney W. Grimesor
37658f0484fSRodney W. Grimes.Fn mergesort
3771fae73b1SRuslan Ermilovfunctions
37858f0484fSRodney W. Grimeswere unable to allocate memory.
37958f0484fSRodney W. Grimes.El
38058f0484fSRodney W. Grimes.Sh SEE ALSO
38158f0484fSRodney W. Grimes.Xr sort 1 ,
38258f0484fSRodney W. Grimes.Xr radixsort 3
38358f0484fSRodney W. Grimes.Rs
38458f0484fSRodney W. Grimes.%A Hoare, C.A.R.
38558f0484fSRodney W. Grimes.%D 1962
38658f0484fSRodney W. Grimes.%T "Quicksort"
38758f0484fSRodney W. Grimes.%J "The Computer Journal"
38858f0484fSRodney W. Grimes.%V 5:1
38958f0484fSRodney W. Grimes.%P pp. 10-15
39058f0484fSRodney W. Grimes.Re
39158f0484fSRodney W. Grimes.Rs
39258f0484fSRodney W. Grimes.%A Williams, J.W.J
39358f0484fSRodney W. Grimes.%D 1964
39458f0484fSRodney W. Grimes.%T "Heapsort"
39558f0484fSRodney W. Grimes.%J "Communications of the ACM"
39658f0484fSRodney W. Grimes.%V 7:1
39758f0484fSRodney W. Grimes.%P pp. 347-348
39858f0484fSRodney W. Grimes.Re
39958f0484fSRodney W. Grimes.Rs
40058f0484fSRodney W. Grimes.%A Knuth, D.E.
40158f0484fSRodney W. Grimes.%D 1968
40258f0484fSRodney W. Grimes.%B "The Art of Computer Programming"
40358f0484fSRodney W. Grimes.%V Vol. 3
40458f0484fSRodney W. Grimes.%T "Sorting and Searching"
40558f0484fSRodney W. Grimes.%P pp. 114-123, 145-149
40658f0484fSRodney W. Grimes.Re
40758f0484fSRodney W. Grimes.Rs
4085e24a424STim J. Robbins.%A McIlroy, P.M.
40958f0484fSRodney W. Grimes.%T "Optimistic Sorting and Information Theoretic Complexity"
41058f0484fSRodney W. Grimes.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
41158f0484fSRodney W. Grimes.%V January 1992
41258f0484fSRodney W. Grimes.Re
41358f0484fSRodney W. Grimes.Rs
41458f0484fSRodney W. Grimes.%A Bentley, J.L.
4155e24a424STim J. Robbins.%A McIlroy, M.D.
41658f0484fSRodney W. Grimes.%T "Engineering a Sort Function"
4175e24a424STim J. Robbins.%J "Software--Practice and Experience"
4185e24a424STim J. Robbins.%V Vol. 23(11)
4195e24a424STim J. Robbins.%P pp. 1249-1265
4205e24a424STim J. Robbins.%D November\ 1993
42158f0484fSRodney W. Grimes.Re
42258f0484fSRodney W. Grimes.Sh STANDARDS
42358f0484fSRodney W. GrimesThe
42458f0484fSRodney W. Grimes.Fn qsort
425*3df1abdfSXin LIfunction conforms to
426588a200cSRuslan Ermilov.St -isoC .
427*3df1abdfSXin LIThe
428*3df1abdfSXin LI.Fn qsort_r
429*3df1abdfSXin LIfunction conforms to
430*3df1abdfSXin LI.St -p1003.1-2024 .
431*3df1abdfSXin LIThe
4320d2fabfcSEdward Tomasz Napierala.Fn qsort_s
433*3df1abdfSXin LIfunction conforms to
4340d2fabfcSEdward Tomasz Napierala.St -isoC-2011
4350d2fabfcSEdward Tomasz NapieralaK.3.6.3.2.
43646cdc140SDavid Chisnall.Sh HISTORY
43746cdc140SDavid ChisnallThe variants of these functions that take blocks as arguments first appeared in
43846cdc140SDavid ChisnallMac OS X.
43946cdc140SDavid ChisnallThis implementation was created by David Chisnall.
440af3c7888SEd Schouten.Pp
441af3c7888SEd SchoutenIn
442af3c7888SEd Schouten.Fx 14.0 ,
443af3c7888SEd Schoutenthe prototype of
444af3c7888SEd Schouten.Fn qsort_r
445*3df1abdfSXin LIwas updated to match
446*3df1abdfSXin LI.St -p1003.1-2024 .
447