Lower bounds for sorting

An inversion within a sequence of values is a pair of values that are "out of order" relative to each other. For example, if we desire to have values in increasing order then for the sequence (3, 14, 1, 5, 9) the pair (3,14) is okay but the pair (3,1) is an inversion.

Theorem.  Any sorting algorithm that removes at most one inversion after each step requires at least n(n-1)/2 steps in the worst case and at least n(n-1)/4 steps on the average.

Proof.  Consider sequences containing the integers I ={ 1,2,...,n }. There are n! such sequences. We define the reverse of sequence A =(a1,a2,...an) to be the sequence AR =(an,an-1,...a1). Note that (AR)R =A, i.e., A is the reverse of AR and, when n > 1, no sequence is its own reverse. Thus the n! sequences can be partitioned into n!/2 sequence couplets, each couplet consisting of a sequence and its reverse.

There are n(n -1)/2 value pairs (i, j) such that i > j and i, j are members of I. For each such value pair (i, j) and for each sequence couplet { A,AR }, (i, j) will be an inversion in exactly one of { A,AR }. Thus, the total number of inversions among both A and AR is exactly n(n-1)/2. Therefore, the average number of inversions is exactly n(n-1)/4.

Theorem.  During a sorting process, the sum of the distances travelled by the records of a random permutation is (n2 -1)/3. Therefore, any sorting algorithm which moves records only a constant number of positions in one step requires Ω(n2) steps.

Proof.  Let A =(a1,a2,...an) be a random permutation of (1,2,...,n). Then |aj - j| is the distance travelled by record j during sorting. aj can equally likely be any of { 1,2,...,n }, so

the expected value of |aj - j| = (1/n) (|1 - j| + |2 - j| + ... + |j - j| + ... + |n - j|)
= (1/n) [ ∑ i =1 to j -1 ( i ) + ∑ i =1 to n- j ( i ) ]
= (1/n) [ (j -1) j/2 + (n- j)(n- j+1)/2 ]

The sum of the distances travelled by the records during sorting is

j =1 to n |aj - j| = (1/n) ∑ j =1 to n [ (j -1) j/2 + (n- j)(n- j+1)/2 ]
(Note that the terms are the same for complementary values of j.)
= (1/n) ∑ j =1 to n [(j -1) j]
= (1/n) ∑ j =1 to n (j 2 - j)
= (1/n) [ n(n+1)(2n+1)/6 - n(n+1)/2 ]
= [n2 -1] / 3

Theorem.  Any algorithm for sorting by comparisons requires Ω(n log n) comparisons.

Proof.  We can model any such algorithm by a decision tree. Since the result of sorting n elements can be any one of the n! permutations of the input, there must be at least n! leaves.

It is an easy proof by induction to show that a binary tree (in which each non-leaf has two sons) of height h has at most 2h leaves.

Therefore, the height of the decision tree must be at least lg(n!).

Since n! ≥ n(n -1)...⌈n/2⌉ ≥ (n/2)n/2, therefore lg(n!) ≥ (n/2) lg (n/2) = 0.5 n lg n - .5n, which is Ω(n log n).

Actually, Stirling's approximation gives us that lg(n!) ≈ n lg n - 1.44 n.


Dan Hirschberg
Last modified: Jul 18, 2006