xref: /csrg-svn/lib/libc/stdlib/qsort.3 (revision 56965)
148350Scael.\" Copyright (c) 1990, 1991 The Regents of the University of California.
242127Sbostic.\" All rights reserved.
320442Smckusick.\"
450315Sbostic.\" This code is derived from software contributed to Berkeley by
550315Sbostic.\" the American National Standards Committee X3, on Information
650315Sbostic.\" Processing Systems.
750315Sbostic.\"
842127Sbostic.\" %sccs.include.redist.man%
920442Smckusick.\"
10*56965Sbostic.\"     @(#)qsort.3	6.9 (Berkeley) 12/02/92
1142127Sbostic.\"
1248350Scael.Dd
1348350Scael.Dt QSORT 3
1448350Scael.Os
1548350Scael.Sh NAME
16*56965Sbostic.Nm qsort, heapsort, mergesort
1749990Sbostic.Nd sort functions
1848350Scael.Sh SYNOPSIS
1948350Scael.Fd #include <stdlib.h>
2048350Scael.Ft void
2148350Scael.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
2249990Sbostic.Ft int
2349990Sbostic.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
24*56965Sbostic.Ft int
25*56965Sbostic.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
2648350Scael.Sh DESCRIPTION
2748350ScaelThe
2848350Scael.Fn qsort
2949990Sbosticfunction is a modified partition-exchange sort, or quicksort.
3049990SbosticThe
3149990Sbostic.Fn heapsort
3249990Sbosticfunction is a modified selection sort.
33*56965SbosticThe
34*56965Sbostic.Fn mergesort
35*56965Sbosticfunction is a modified merge sort with exponential search
36*56965Sbosticintended for sorting data with pre-existing order.
3748350Scael.Pp
3842127SbosticThe
3948350Scael.Fn qsort
4049990Sbosticand
4149990Sbostic.Fn heapsort
4249990Sbosticfunctions sort an array of
4348350Scael.Fa nmemb
4442127Sbosticobjects, the initial member of which is pointed to by
4548350Scael.Fa base .
4642127SbosticThe size of each object is specified by
4748350Scael.Fa size .
48*56965Sbostic.Fn Mergesort
49*56965Sbosticbehaves similarly, but
50*56965Sbostic.Em requires
51*56965Sbosticthat
52*56965Sbostic.Fa size
53*56965Sbosticbe greater than
54*56965Sbostic.Dq "sizeof(void *) / 2" .
5548350Scael.Pp
56*56965SbosticThe contents of the array
57*56965Sbostic.Fa base
58*56965Sbosticare sorted in ascending order according to
5942127Sbostica comparison function pointed to by
6048350Scael.Fa compar ,
61*56965Sbosticwhich requires two arguments pointing to the objects being
6242127Sbosticcompared.
6348350Scael.Pp
6442127SbosticThe comparison function must return an integer less than, equal to, or
6542127Sbosticgreater than zero if the first argument is considered to be respectively
6620442Smckusickless than, equal to, or greater than the second.
6748350Scael.Pp
6849990SbosticThe functions
6948350Scael.Fn qsort
7049990Sbosticand
7149990Sbostic.Fn heapsort
7249990Sbosticare
7348350Scael.Em not
7445643Sbosticstable, that is, if two members compare as equal, their order in
7545643Sbosticthe sorted array is undefined.
76*56965SbosticThe function
77*56965Sbostic.Fn mergesort
78*56965Sbosticis stable.
7948350Scael.Pp
8048350ScaelThe
8148350Scael.Fn qsort
8249990Sbosticfunction is an implementation of C.A.R. Hoare's ``quicksort'' algorithm,
8349990Sbostica variant of partition-exchange sorting; in particular, see D.E. Knuth's
8449990SbosticAlgorithm Q.
8548350Scael.Fn Qsort
8645643Sbostictakes O N lg N average time.
87*56965SbosticThis implementation uses median selection to avoid its
8849990SbosticO N**2 worst-case behavior.
8949990Sbostic.Pp
9049990SbosticThe
9149990Sbostic.Fn heapsort
9249990Sbosticfunction is an implementation of J.W.J. William's ``heapsort'' algorithm,
9349990Sbostica variant of selection sorting; in particular, see D.E. Knuth's Algorithm H.
9449990Sbostic.Fn Heapsort
9549990Sbostictakes O N lg N worst-case time.
9649990SbosticIts
9749990Sbostic.Em only
9849990Sbosticadvantage over
9949990Sbostic.Fn qsort
10051175Sbosticis that it uses almost no additional memory; while
101*56965Sbostic.Fn qsort
10251175Sbosticdoes not allocate memory, it is implemented using recursion.
103*56965Sbostic.Pp
104*56965SbosticThe function
105*56965Sbostic.Fn mergesort
106*56965Sbosticrequires additional memory of size
107*56965Sbostic.Fa nmemb *
108*56965Sbostic.Fa size
109*56965Sbosticbytes; it should be used only when space is not at a premium.
110*56965Sbostic.Fn Mergesort
111*56965Sbosticis optimized for data with pre-existing order; its worst case
112*56965Sbostictime is O N lg N; its best case is O N.
113*56965Sbostic.Pp
114*56965SbosticNormally,
115*56965Sbostic.Fn qsort
116*56965Sbosticis faster than
117*56965Sbostic.Fn mergesort
118*56965Sbosticis faster than
119*56965Sbostic.Fn heapsort .
120*56965SbosticMemory availability and pre-existing order in the data can make this
121*56965Sbosticuntrue.
12248350Scael.Sh RETURN VALUES
12348350ScaelThe
12448350Scael.Fn qsort
12548350Scaelfunction
12645643Sbosticreturns no value.
12749990Sbostic.Pp
12849990SbosticUpon successful completion,
12949990Sbostic.Fn heapsort
130*56965Sbosticand
131*56965Sbostic.Fn mergesort
132*56965Sbosticreturn 0.
133*56965SbosticOtherwise, they return \-1 and the global variable
13449990Sbostic.Va errno
13549990Sbosticis set to indicate the error.
13649990Sbostic.Sh ERRORS
13749990SbosticThe
13849990Sbostic.Fn heapsort
13949990Sbosticfunction succeeds unless:
14049990Sbostic.Bl -tag -width Er
14149990Sbostic.It Bq Er EINVAL
14249990SbosticThe
14349990Sbostic.Fa size
144*56965Sbosticargument is zero, or,
145*56965Sbosticthe
146*56965Sbostic.Fa size
147*56965Sbosticargument to
148*56965Sbostic.Fn mergesort
149*56965Sbosticis less than
150*56965Sbostic.Dq "sizeof(void *) / 2" .
15151175Sbostic.It Bq Er ENOMEM
152*56965Sbostic.Fn Heapsort
153*56965Sbosticor
154*56965Sbostic.Fn mergesort
155*56965Sbosticwere unable to allocate memory.
156*56965Sbostic.El
15748350Scael.Sh COMPATIBILITY
15845643SbosticPrevious versions of
15948350Scael.Fn qsort
160*56965Sbosticdid not permit the comparison routine itself to call
16148350Scael.Fn qsort 3 .
16245643SbosticThis is no longer true.
16348350Scael.Sh SEE ALSO
16448350Scael.Xr sort 1 ,
16548350Scael.Xr radixsort 3
16648350Scael.Rs
16748350Scael.%A Hoare, C.A.R.
16848350Scael.%D 1962
16948350Scael.%T "Quicksort"
17048350Scael.%J "The Computer Journal"
17148350Scael.%V 5:1
17248350Scael.%P pp. 10-15
17348350Scael.Re
17449990Sbostic.%A Williams, J.W.J
17549990Sbostic.%D 1964
17649990Sbostic.%T "Heapsort"
17749990Sbostic.%J "Communications of the ACM"
17849990Sbostic.%V 7:1
17949990Sbostic.%P pp. 347-348
18049990Sbostic.Re
18149990Sbostic.Rs
18248350Scael.%A Knuth, D.E.
18348350Scael.%D 1968
18448350Scael.%B "The Art of Computer Programming"
18548350Scael.%V Vol. 3
18648350Scael.%T "Sorting and Searching"
18749990Sbostic.%P pp. 114-123, 145-149
18848350Scael.Re
189*56965Sbostic.Rs
190*56965Sbostic.%A Mcilroy, P.M.
191*56965Sbostic.%T "Optimistic Sorting and Information Theoretic Complexity"
192*56965Sbostic.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms"
193*56965Sbostic.%V January 1992
194*56965Sbostic.Re
19548350Scael.Sh STANDARDS
19648350ScaelThe
19748350Scael.Fn qsort
19848350Scaelfunction
19948350Scaelconforms to
20048350Scael.St -ansiC .
201