1.\" Copyright (c) 1990, 1991 The Regents of the University of California. 2.\" All rights reserved. 3.\" 4.\" This code is derived from software contributed to Berkeley by 5.\" the American National Standards Committee X3, on Information 6.\" Processing Systems. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. All advertising materials mentioning features or use of this software 17.\" must display the following acknowledgement: 18.\" This product includes software developed by the University of 19.\" California, Berkeley and its contributors. 20.\" 4. Neither the name of the University nor the names of its contributors 21.\" may be used to endorse or promote products derived from this software 22.\" without specific prior written permission. 23.\" 24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34.\" SUCH DAMAGE. 35.\" 36.\" from: @(#)qsort.3 6.7 (Berkeley) 6/29/91 37.\" $Id: qsort.3,v 1.2 1993/08/01 07:44:22 mycroft Exp $ 38.\" 39.Dd June 29, 1991 40.Dt QSORT 3 41.Os 42.Sh NAME 43.Nm qsort, heapsort 44.Nd sort functions 45.Sh SYNOPSIS 46.Fd #include <stdlib.h> 47.Ft void 48.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" 49.Ft int 50.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" 51.Sh DESCRIPTION 52The 53.Fn qsort 54function is a modified partition-exchange sort, or quicksort. 55The 56.Fn heapsort 57function is a modified selection sort. 58.Pp 59The 60.Fn qsort 61and 62.Fn heapsort 63functions sort an array of 64.Fa nmemb 65objects, the initial member of which is pointed to by 66.Fa base . 67The size of each object is specified by 68.Fa size . 69.Pp 70The contents of the array are sorted in ascending order according to 71a comparison function pointed to by 72.Fa compar , 73which is called with two arguments that point to the objects being 74compared. 75.Pp 76The comparison function must return an integer less than, equal to, or 77greater than zero if the first argument is considered to be respectively 78less than, equal to, or greater than the second. 79.Pp 80The functions 81.Fn qsort 82and 83.Fn heapsort 84are 85.Em not 86stable, that is, if two members compare as equal, their order in 87the sorted array is undefined. 88.Pp 89The 90.Fn qsort 91function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, 92a variant of partition-exchange sorting; in particular, see D.E. Knuth's 93Algorithm Q. 94.Fn Qsort 95takes O N lg N average time. 96This implementation uses median selection to avoid the traditional 97O N**2 worst-case behavior. 98.Pp 99The 100.Fn heapsort 101function is an implementation of J.W.J. William's ``heapsort'' algorithm, 102a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. 103.Fn Heapsort 104takes O N lg N worst-case time. 105Its 106.Em only 107advantage over 108.Fn qsort 109is that it uses no additional memory. 110.Sh RETURN VALUES 111The 112.Fn qsort 113function 114returns no value. 115.Pp 116Upon successful completion, 117.Fn heapsort 118returns 0. 119Otherwise, it returns \-1 and the global variable 120.Va errno 121is set to indicate the error. 122.Sh ERRORS 123The 124.Fn heapsort 125function succeeds unless: 126.Bl -tag -width Er 127.It Bq Er EINVAL 128The 129.Fa size 130argument is zero. 131.Sh COMPATIBILITY 132Previous versions of 133.Fn qsort 134did not permit the comparison routine to itself call 135.Fn qsort 3 . 136This is no longer true. 137.Sh SEE ALSO 138.Xr sort 1 , 139.Xr radixsort 3 140.Rs 141.%A Hoare, C.A.R. 142.%D 1962 143.%T "Quicksort" 144.%J "The Computer Journal" 145.%V 5:1 146.%P pp. 10-15 147.Re 148.Rs 149.%A Williams, J.W.J 150.%D 1964 151.%T "Heapsort" 152.%J "Communications of the ACM" 153.%V 7:1 154.%P pp. 347-348 155.Re 156.Rs 157.%A Knuth, D.E. 158.%D 1968 159.%B "The Art of Computer Programming" 160.%V Vol. 3 161.%T "Sorting and Searching" 162.%P pp. 114-123, 145-149 163.Re 164.Sh STANDARDS 165The 166.Fn qsort 167function 168conforms to 169.St -ansiC . 170