qsort is a
C standard library
The C standard library, sometimes referred to as libc, is the standard library for the C (programming language), C programming language, as specified in the ISO C standard.International Organization for Standardization, ISO/International Electrote ...
function that implements a
sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a List (computing), list into an Total order, order. The most frequently used orders are numerical order and lexicographical order, and either ascending or descending ...
for arrays of arbitrary objects according to a user-provided comparison function. It is named after the "quicker sort" algorithm
(a
quicksort
Quicksort is an efficient, general-purpose sorting algorithm. Quicksort was developed by British computer scientist Tony Hoare in 1959 and published in 1961. It is still a commonly used algorithm for sorting. Overall, it is slightly faster than ...
variant due to R. S. Scowen), which was originally used to implement it in the
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
C library, although the C standard does not require it to implement quicksort.
The ability to operate on different kinds of data (''
polymorphism'') is achieved by taking a
function pointer
A function pointer, also called a subroutine pointer or procedure pointer, is a pointer referencing executable code, rather than data. Dereferencing the function pointer yields the referenced function, which can be invoked and passed arguments ...
to a
three-way comparison function, as well as a parameter that specifies the size of its individual input objects. The
C standard requires the comparison function to implement a
total order
In mathematics, a total order or linear order is a partial order in which any two elements are comparable. That is, a total order is a binary relation \leq on some set X, which satisfies the following for all a, b and c in X:
# a \leq a ( re ...
on the items in the input array.
History
A qsort function appears in
Version 2 Unix in 1972 as a library
assembly language
In computing, assembly language (alternatively assembler language or symbolic machine code), often referred to simply as assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence bet ...
subroutine. Its interface is unlike the modern version, in that it can be pseudo-prototyped as
qsort(void * start, void * end, unsigned length)
– sorting contiguously-stored length-long byte strings from the range
This, and the lack of a replaceable comparison function, makes it unsuitable to properly sort the system's little-endian">''start, end). This, and the lack of a replaceable comparison function, makes it unsuitable to properly sort the system's little-endian integers, or any other data structures.
In Version 3 Unix, the interface is extended by calling compar(III), with an interface identical to modern-day memcmp. This function may be overridden by the user's program to implement any kind of ordering, in an equivalent fashion to the
compar
argument to standard qsort (though program-global, of course).
Version 4 Unix
Research Unix refers to the early versions of the Unix operating system for PDP-7, DEC PDP-7, PDP-11, VAX and Interdata 7/32 and 8/32 computers, developed in the Bell Labs Computing Sciences Research Center (CSRC). The term ''Research Unix'' first ...
adds a C implementation, with an interface equivalent to the standard.
It was rewritten in 1983 for the
Berkeley Software Distribution
The Berkeley Software Distribution (BSD), also known as Berkeley Unix or BSD Unix, is a discontinued Unix operating system developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berkeley, beginn ...
.
The function was standardized in
ANSI C
ANSI C, ISO C, and Standard C are successive standards for the C programming language published by the American National Standards Institute (ANSI) and ISO/IEC JTC 1/SC 22/WG 14 of the International Organization for Standardization (ISO) and the ...
(1989).
The assembly implementation is removed in
Version 6 Unix.
In 1991, Bell Labs employees observed that AT&T and BSD versions of qsort would consume
quadratic time
In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations p ...
for some simple inputs. Thus
Jon Bentley and
Douglas McIlroy
Malcolm Douglas McIlroy (born 1932) is an American mathematician, engineer, and programmer. As of 2019 he is an Adjunct Professor of Computer Science at Dartmouth College.
McIlroy is best known for having originally proposed Unix pipelines and de ...
engineered a new faster and more robust implementation.
McIlroy would later produce a more complex quadratic-time input, termed ''AntiQuicksort'', in 1998. This function constructs adversarial data on-the-fly.
Example
The following piece of C code shows how to sort a list of integers using qsort.
#include
/* Comparison function. Receives two generic (void) pointers to the items under comparison. */
int compare_ints(const void *p, const void *q)
/* Sort an array of n integers, pointed to by a. */
void sort_ints(int *a, size_t n)
Extensions
Since the comparison function of the original
qsort
only accepts two pointers, passing in additional parameters (e.g. producing a comparison function that compares by the two value's difference with another value) must be done using
global variables. The issue was solved by the
BSD
The Berkeley Software Distribution (BSD), also known as Berkeley Unix or BSD Unix, is a discontinued Unix operating system developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berkeley, beginni ...
and
GNU Unix-like
A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems by introducing a
qsort_r
function, which allows for an additional parameter to be passed to the comparison function. The two versions of
qsort_r
have different argument orders.
C11 Annex K defines a
qsort_s
essentially identical to GNU's
qsort_r
. The
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
and
FreeBSD
FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
libcs also contain
qsort_b
, a variant that uses
blocks, an analogue to
closures, as an alternate solution to the same problem.
References
{{reflist
C standard library
Sorting algorithms