TOPICS
Search

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 {a_i}_(i=1)^n, the partitioning rules are (Sedgewick 1978):

1. Some key nu is in its final position in the array (i.e., if it is the jth smallest, it is in position j, so a_j=nu).

2. All the elements to the left of a_j are less than or equal to a_j. The elements a_1, a_2, ..., a_(j-1) are called the "left subfile."

3. All the elements to the right of a_j are greater than or equal to a_j. The elements a_(j+1), ..., a_n 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 O(n^2) 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 n costs n units, consisting of one unit to select an element and n-1 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 T_n of the total cost then satisfies the recurrence equation

 T_n=n+2/nsum_(k=0)^(n-1)T_k
(1)

for a positive integer n, with T_0=0 (Havil 2003, p. 129). This recurrence equation can be rewritten as

 nT_n=(n+1)T_(n-1)+2n-1,
(2)

which has solution

 T_n=2(n+1)H_n-3n,
(3)

where H_n is a harmonic number, with H_0=0. For n=0, 1, ..., the first few values are 0, 1, 3, 17/3, 53/6, 62/5, 163/10, ... (OEIS A093418 and A096620).

If only the n-1 comparisons are counted, the corresponding expectation value is 2(n+1)H_n-4n (Ekhad and Zeilberger 2019, pp. 3-4).

The cost T_n has the asymptotic expansion

 T_n=1-3n+2(n+1)gamma+5/(6n)+2(n+1)lnn+O(n^(-2)),
(4)

where gamma is the Euler-Mascheroni constant. Thus T_n∼2nlnn, and in asymptotic notation T_n=O(nlnn) (Havil 2003, p. 130).


See also

Heapsort, Sorting

Explore 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

Quicksort

Cite this as:

Weisstein, Eric W. "Quicksort." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/Quicksort.html

Subject classifications