Quicksort
Quicksort is a comparison-based sorting algorithm that uses recursion to sort the two parts obtained
by partitioning an array around a selected element. For
an array , the partitioning rules are (Sedgewick 1978):
1. Some key
is in its final position in the array (i.e., if it is the
th smallest, it is in position
, so
).
2. All the elements to the left of are less than or equal to
. The elements
,
,
...,
are called the "left subfile."
3. All the elements to the right of are greater than or equal to
. The elements
, ...,
are called the "right subfile."
The left and right subfiles are then sorted by the same algorithm. Quicksort was invented by Hoare (1961, 1962) and has undergone extensive analysis
(Sedgewick 1975, 1977, 1978). Its worst-case cost is when the successive partitions are maximally unbalanced.
An already sorted array produces this behavior when the
first or last element is always selected.
Consider the cost model in which processing a nonempty subfile of length costs
units, consisting of one unit to select an element and
comparisons to partition the remaining
elements. Suppose the keys are distinct and the selected element is chosen uniformly
at random in each subfile. The expectation value
of the total cost then satisfies the
recurrence equation
|
(1)
|
for a positive integer , with
(Havil 2003, p. 129). This recurrence
equation can be rewritten as
|
(2)
|
which has solution
|
(3)
|
where is a harmonic
number, with
.
For
, 1, ..., the first few values are
0, 1, 3, 17/3, 53/6, 62/5, 163/10, ... (OEIS A093418
and A096620).
If only the
comparisons are counted, the corresponding expectation
value is
(Ekhad and Zeilberger 2019, pp. 3-4).
The cost
has the asymptotic expansion
|
(4)
|
where is the Euler-Mascheroni
constant. Thus
,
and in asymptotic notation
(Havil 2003, p. 130).
See also
Heapsort, SortingExplore with Wolfram|Alpha
References
Aho, A. V.; Hopcroft, J. E.; and Ullman, J. D. Data Structures and Algorithms. Reading, MA: Addison-Wesley, pp. 260-270, 1987.Ekhad, S. B. and Zeilberger, D. "A Detailed Analysis of Quicksort Running Time." 9 Mar 2019. https://arxiv.org/abs/1903.03708.Havil, J. "Quicksort." §13.8 in Gamma: Exploring Euler's Constant. Princeton, NJ: Princeton University Press, pp. 128-130, 2003.Hoare, C. A. R. "Partition: Algorithm 63," "Quicksort: Algorithm 64," and "Find: Algorithm 65." Comm. ACM 4, 321-322, 1961.Hoare, C. A. R. "Quicksort." Computer J. 5, 10-15, 1962. https://doi.org/10.1093/comjnl/5.1.10.Knuth, D. E. The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed. Reading, MA: Addison-Wesley, pp. 113-122, 1998.Press, W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T. "Quicksort." §8.2 in Numerical Recipes in FORTRAN: The Art of Scientific Computing, 2nd ed. Cambridge, England: Cambridge University Press, pp. 323-327, 1992.Sedgewick, R. "Quicksort." PhD thesis. Stanford Computer Science Report STAN-CS-75-492. Stanford, CA: Stanford University, May 1975.Sedgewick, R. "The Analysis of Quicksort Programs." Acta Informatica 7, 327-355, 1977.Sedgewick, R. "Implementing Quicksort Programs." Comm. ACM 21, 847-857, 1978.Sedgewick, R. Quicksort. New York: Garland, 1980. Reprint of the author's 1975 thesis. https://sedgewick.io/wp-content/themes/sedgewick/papers/1975Quicksort.pdf.Sloane, N. J. A. Sequences A093418 and A096620 in "The On-Line Encyclopedia of Integer Sequences."Referenced on Wolfram|Alpha
QuicksortCite this as:
Weisstein, Eric W. "Quicksort." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/Quicksort.html