macro and text revision (-mdoc version 3)
[unix-history] / usr / src / lib / libc / stdlib / qsort.3
.\" Copyright (c) 1990, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" %sccs.include.redist.man%
.\"
.\" @(#)qsort.3 6.5 (Berkeley) %G%
.\"
.Dd
.Dt QSORT 3
.Os
.Sh NAME
.Nm qsort
.Nd quicker sort
.Sh SYNOPSIS
.Fd #include <stdlib.h>
.Ft void
.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)"
.Sh DESCRIPTION
The
.Fn qsort
function
is a modified partition-exchange sort, or quicksort.
.Pp
The
.Fn qsort
function sorts an array of
.Fa nmemb
objects, the initial member of which is pointed to by
.Fa base .
The size of each object is specified by
.Fa size .
.Pp
The contents of the array are sorted in ascending order according to
a comparison function pointed to by
.Fa compar ,
which is called with two arguments that point to the objects being
compared.
.Pp
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be respectively
less than, equal to, or greater than the second.
.Pp
The
.Fn qsort
function
is
.Em not
stable, that is, if two members compare as equal, their order in
the sorted array is undefined.
.Pp
The
.Fn qsort
function
is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, a variant
of partition-exchange sorting; in particular, see D.E. Knuth's Algorithm Q.
.Fn Qsort
takes O N lg N average time.
.Sh RETURN VALUES
The
.Fn qsort
function
returns no value.
.Sh COMPATIBILITY
Previous versions of
.Fn qsort
did not permit the comparison routine to itself call
.Fn qsort 3 .
This is no longer true.
.Sh SEE ALSO
.Xr sort 1 ,
.Xr radixsort 3
.Rs
.%A Hoare, C.A.R.
.%D 1962
.%T "Quicksort"
.%J "The Computer Journal"
.%V 5:1
.%P pp. 10-15
.Re
.Rs
.%A Knuth, D.E.
.%D 1968
.%B "The Art of Computer Programming"
.%V Vol. 3
.%T "Sorting and Searching"
.%P pp. 114-123
.Re
.Sh STANDARDS
The
.Fn qsort
function
conforms to
.St -ansiC .