xref: /freebsd/lib/libc/stdlib/qsort.3 (revision af3c78886fd8d4ca5eebdbe581a459a6f6d29d6a)
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*af3c7888SEd Schouten.Dd September 30, 2022
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"
70*af3c7888SEd 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 ,
160*af3c7888SEd 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
2630d2fabfcSEdward Tomasz Napieralaif
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
2730d2fabfcSEdward Tomasz Napieralais NULL, then the runtime-constraint handler is called, and
2740d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2750d2fabfcSEdward Tomasz Napieralareturns an error.
2760d2fabfcSEdward Tomasz NapieralaNote that the handler is called before
2770d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2780d2fabfcSEdward Tomasz Napieralareturns the error, and the handler function might not return.
2790d2fabfcSEdward Tomasz Napierala.El
28058f0484fSRodney W. Grimes.Sh RETURN VALUES
28158f0484fSRodney W. GrimesThe
28258f0484fSRodney W. Grimes.Fn qsort
283eca67d51SGarrett Wollmanand
284eca67d51SGarrett Wollman.Fn qsort_r
285eca67d51SGarrett Wollmanfunctions
286eca67d51SGarrett Wollmanreturn no value.
2870d2fabfcSEdward Tomasz NapieralaThe
2880d2fabfcSEdward Tomasz Napierala.Fn qsort_s
2890d2fabfcSEdward Tomasz Napieralafunction returns zero on success, non-zero on error.
29058f0484fSRodney W. Grimes.Pp
291d6002fefSRuslan Ermilov.Rv -std heapsort mergesort
2928ce3e01eSGiorgos Keramidas.Sh EXAMPLES
2938ce3e01eSGiorgos KeramidasA sample program that sorts an array of
2948ce3e01eSGiorgos Keramidas.Vt int
2958ce3e01eSGiorgos Keramidasvalues in place using
2968ce3e01eSGiorgos Keramidas.Fn qsort ,
2978ce3e01eSGiorgos Keramidasand then prints the sorted array to standard output is:
2988ce3e01eSGiorgos Keramidas.Bd -literal
2998ce3e01eSGiorgos Keramidas#include <stdio.h>
3008ce3e01eSGiorgos Keramidas#include <stdlib.h>
3018ce3e01eSGiorgos Keramidas
3028ce3e01eSGiorgos Keramidas/*
30348ac3a2aSSergey Kandaurov * Custom comparison function that compares 'int' values through pointers
3048ce3e01eSGiorgos Keramidas * passed by qsort(3).
3058ce3e01eSGiorgos Keramidas */
3068ce3e01eSGiorgos Keramidasstatic int
3078ce3e01eSGiorgos Keramidasint_compare(const void *p1, const void *p2)
3088ce3e01eSGiorgos Keramidas{
309302318d5SGiorgos Keramidas	int left = *(const int *)p1;
310302318d5SGiorgos Keramidas	int right = *(const int *)p2;
3118ce3e01eSGiorgos Keramidas
312302318d5SGiorgos Keramidas	return ((left > right) - (left < right));
3138ce3e01eSGiorgos Keramidas}
3148ce3e01eSGiorgos Keramidas
3158ce3e01eSGiorgos Keramidas/*
3168ce3e01eSGiorgos Keramidas * Sort an array of 'int' values and print it to standard output.
3178ce3e01eSGiorgos Keramidas */
3188ce3e01eSGiorgos Keramidasint
3198ce3e01eSGiorgos Keramidasmain(void)
3208ce3e01eSGiorgos Keramidas{
3218ce3e01eSGiorgos Keramidas	int int_array[] = { 4, 5, 9, 3, 0, 1, 7, 2, 8, 6 };
32248ac3a2aSSergey Kandaurov	size_t array_size = sizeof(int_array) / sizeof(int_array[0]);
323302318d5SGiorgos Keramidas	size_t k;
3248ce3e01eSGiorgos Keramidas
325302318d5SGiorgos Keramidas	qsort(&int_array, array_size, sizeof(int_array[0]), int_compare);
3268ce3e01eSGiorgos Keramidas	for (k = 0; k < array_size; k++)
3278ce3e01eSGiorgos Keramidas		printf(" %d", int_array[k]);
328302318d5SGiorgos Keramidas	puts("");
329302318d5SGiorgos Keramidas	return (EXIT_SUCCESS);
3308ce3e01eSGiorgos Keramidas}
3318ce3e01eSGiorgos Keramidas.Ed
332954349a6SJoel Dahl.Sh COMPATIBILITY
3330d2fabfcSEdward Tomasz NapieralaThe order of arguments for the comparison function used with
3340d2fabfcSEdward Tomasz Napierala.Fn qsort_r
3350d2fabfcSEdward Tomasz Napieralais different from the one used by
3360d2fabfcSEdward Tomasz Napierala.Fn qsort_s ,
3370d2fabfcSEdward Tomasz Napieralaand the GNU libc implementation of
3380d2fabfcSEdward Tomasz Napierala.Fn qsort_r .
3390d2fabfcSEdward Tomasz NapieralaWhen porting software written for GNU libc, it is usually possible
3400d2fabfcSEdward Tomasz Napieralato replace
3410d2fabfcSEdward Tomasz Napierala.Fn qsort_r
3420d2fabfcSEdward Tomasz Napieralawith
3430d2fabfcSEdward Tomasz Napierala.Fn qsort_s
3440d2fabfcSEdward Tomasz Napieralato work around this problem.
3450d2fabfcSEdward Tomasz Napierala.Pp
346ae39ed86SConrad Meyer.Fn qsort_s
347ae39ed86SConrad Meyeris part of the
348ae39ed86SConrad Meyer.Em optional
349ae39ed86SConrad MeyerAnnex K portion of
350ae39ed86SConrad Meyer.St -isoC-2011
351ae39ed86SConrad Meyerand may not be portable to other standards-conforming platforms.
352ae39ed86SConrad Meyer.Pp
353954349a6SJoel DahlPrevious versions of
354954349a6SJoel Dahl.Fn qsort
355954349a6SJoel Dahldid not permit the comparison routine itself to call
356954349a6SJoel Dahl.Fn qsort 3 .
357954349a6SJoel DahlThis is no longer true.
35858f0484fSRodney W. Grimes.Sh ERRORS
35958f0484fSRodney W. GrimesThe
36058f0484fSRodney W. Grimes.Fn heapsort
3618d2c3c32SRobert Nordierand
3628d2c3c32SRobert Nordier.Fn mergesort
3638d2c3c32SRobert Nordierfunctions succeed unless:
36458f0484fSRodney W. Grimes.Bl -tag -width Er
36558f0484fSRodney W. Grimes.It Bq Er EINVAL
36658f0484fSRodney W. GrimesThe
36758f0484fSRodney W. Grimes.Fa size
36858f0484fSRodney W. Grimesargument is zero, or,
36958f0484fSRodney W. Grimesthe
37058f0484fSRodney W. Grimes.Fa size
37158f0484fSRodney W. Grimesargument to
37258f0484fSRodney W. Grimes.Fn mergesort
37358f0484fSRodney W. Grimesis less than
37458f0484fSRodney W. Grimes.Dq "sizeof(void *) / 2" .
37558f0484fSRodney W. Grimes.It Bq Er ENOMEM
3761fae73b1SRuslan ErmilovThe
3771fae73b1SRuslan Ermilov.Fn heapsort
37858f0484fSRodney W. Grimesor
37958f0484fSRodney W. Grimes.Fn mergesort
3801fae73b1SRuslan Ermilovfunctions
38158f0484fSRodney W. Grimeswere unable to allocate memory.
38258f0484fSRodney W. Grimes.El
38358f0484fSRodney W. Grimes.Sh SEE ALSO
38458f0484fSRodney W. Grimes.Xr sort 1 ,
38558f0484fSRodney W. Grimes.Xr radixsort 3
38658f0484fSRodney W. Grimes.Rs
38758f0484fSRodney W. Grimes.%A Hoare, C.A.R.
38858f0484fSRodney W. Grimes.%D 1962
38958f0484fSRodney W. Grimes.%T "Quicksort"
39058f0484fSRodney W. Grimes.%J "The Computer Journal"
39158f0484fSRodney W. Grimes.%V 5:1
39258f0484fSRodney W. Grimes.%P pp. 10-15
39358f0484fSRodney W. Grimes.Re
39458f0484fSRodney W. Grimes.Rs
39558f0484fSRodney W. Grimes.%A Williams, J.W.J
39658f0484fSRodney W. Grimes.%D 1964
39758f0484fSRodney W. Grimes.%T "Heapsort"
39858f0484fSRodney W. Grimes.%J "Communications of the ACM"
39958f0484fSRodney W. Grimes.%V 7:1
40058f0484fSRodney W. Grimes.%P pp. 347-348
40158f0484fSRodney W. Grimes.Re
40258f0484fSRodney W. Grimes.Rs
40358f0484fSRodney W. Grimes.%A Knuth, D.E.
40458f0484fSRodney W. Grimes.%D 1968
40558f0484fSRodney W. Grimes.%B "The Art of Computer Programming"
40658f0484fSRodney W. Grimes.%V Vol. 3
40758f0484fSRodney W. Grimes.%T "Sorting and Searching"
40858f0484fSRodney W. Grimes.%P pp. 114-123, 145-149
40958f0484fSRodney W. Grimes.Re
41058f0484fSRodney W. Grimes.Rs
4115e24a424STim J. Robbins.%A McIlroy, P.M.
41258f0484fSRodney W. Grimes.%T "Optimistic Sorting and Information Theoretic Complexity"
41358f0484fSRodney W. Grimes.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
41458f0484fSRodney W. Grimes.%V January 1992
41558f0484fSRodney W. Grimes.Re
41658f0484fSRodney W. Grimes.Rs
41758f0484fSRodney W. Grimes.%A Bentley, J.L.
4185e24a424STim J. Robbins.%A McIlroy, M.D.
41958f0484fSRodney W. Grimes.%T "Engineering a Sort Function"
4205e24a424STim J. Robbins.%J "Software--Practice and Experience"
4215e24a424STim J. Robbins.%V Vol. 23(11)
4225e24a424STim J. Robbins.%P pp. 1249-1265
4235e24a424STim J. Robbins.%D November\ 1993
42458f0484fSRodney W. Grimes.Re
42558f0484fSRodney W. Grimes.Sh STANDARDS
42658f0484fSRodney W. GrimesThe
42758f0484fSRodney W. Grimes.Fn qsort
42858f0484fSRodney W. Grimesfunction
42958f0484fSRodney W. Grimesconforms to
430588a200cSRuslan Ermilov.St -isoC .
4310d2fabfcSEdward Tomasz Napierala.Fn qsort_s
4320d2fabfcSEdward Tomasz Napieralaconforms to
4330d2fabfcSEdward Tomasz Napierala.St -isoC-2011
4340d2fabfcSEdward Tomasz NapieralaK.3.6.3.2.
43546cdc140SDavid Chisnall.Sh HISTORY
43646cdc140SDavid ChisnallThe variants of these functions that take blocks as arguments first appeared in
43746cdc140SDavid ChisnallMac OS X.
43846cdc140SDavid ChisnallThis implementation was created by David Chisnall.
439*af3c7888SEd Schouten.Pp
440*af3c7888SEd SchoutenIn
441*af3c7888SEd Schouten.Fx 14.0 ,
442*af3c7888SEd Schoutenthe prototype of
443*af3c7888SEd Schouten.Fn qsort_r
444*af3c7888SEd Schoutenwas updated to match POSIX.
445