quicksort
Quicksort^{} is a divideandconquer algorithm^{} for sorting in the comparison model. Its expected running time is $O(n\mathrm{lg}n)$ for sorting $n$ values.
Algorithm
Quicksort can be implemented recursively, as follows:
Algorithm Quicksort($\mathrm{L}$)
Input: A list $L$ of $n$ elements
Output: The list $L$ in sorted order
if $n>1$ then
$p\leftarrow \text{random element of}L$
$$
$B\leftarrow \{zz\in L,z=p\}$
$C\leftarrow \{yy\in L,y>p\}$
${S}_{A}\leftarrow Quicksort(A)$
${S}_{C}\leftarrow Quicksort(C)$
return $Concatenate({S}_{A},B,{S}_{C})$
else
return $L$
Analysis
The behavior of quicksort can be analyzed by considering the computation as a binary tree^{}. Each node of the tree corresponds to one recursive call to the quicksort procedure.
Consider the initial input to the algorithm, some list $L$. Call the Sorted list $S,$ with $i$th and $j$th elements ${S}_{i}$ and ${S}_{j}.$ These two elements will be compared with some probability ${p}_{ij}$. This probability can be determined by considering two preconditions on ${S}_{i}$ and ${S}_{j}$ being compared:

•
${S}_{i}$ or ${S}_{j}$ must be chosen as a pivot $p$, since comparisons only occur against the pivot.

•
No element between ${S}_{i}$ and ${S}_{j}$ can have already been chosen as a pivot before ${S}_{i}$ or ${S}_{j}$ is chosen. Otherwise, would be separated into different sublists in the recursion.
The probability of any particular element being chosen as the pivot is uniform. Therefore, the chance that ${S}_{i}$ or ${S}_{j}$ is chosen as the pivot before any element between them is $2/(ji+1)$. This is precisely ${p}_{ij}.$
The expected number of comparisons is just the summation over all possible comparisons of the probability of that particular comparison occurring. By linearity of expectation, no independence assumptions are necessary. The expected number of comparisons is therefore
$\sum _{i=1}^{n}}{\displaystyle \sum _{j>i}^{n}}{p}_{ij$  $=$  $\sum _{i=1}^{n}}{\displaystyle \sum _{j>i}^{n}}{\displaystyle \frac{2}{ji+1}$  (1)  
$=$  $\sum _{i=1}^{n}}{\displaystyle \sum _{k=2}^{ni+1}}{\displaystyle \frac{2}{k}$  (2)  
$\le $  $2{\displaystyle \sum _{i=1}^{n}}{\displaystyle \sum _{k=1}^{n}}{\displaystyle \frac{1}{k}}$  (3)  
$=$  $2n{H}_{n}=O(n\mathrm{lg}n),$  (4) 
where ${H}_{n}$ is the $n$th Harmonic number.
The worst case behavior is $\mathrm{\Theta}({n}^{2})$, but this almost never occurs (with high probability it does not occur) with random pivots.
Title  quicksort 

Canonical name  Quicksort 
Date of creation  20130322 13:59:19 
Last modified on  20130322 13:59:19 
Owner  thouis (3290) 
Last modified by  thouis (3290) 
Numerical id  13 
Author  thouis (3290) 
Entry type  Definition 
Classification  msc 68P10 
Related topic  SortingProblem 