A lower limit on comparison based sorting.
Objectives
We would like to :
- Establish a lower bound on comparison based sorting.
Notes
- This is from clrs.
- A decision tree is the binary tree that represents the "decisions" that must be made to reach a conclusion.
- In a decision tree, the leaves are "conclusions"
- The inner nodes are the decisions required to reach those conclusions.
- The tree is binary, because the questions are all yes-no.
- It is reasonable to model comparison based sorting with a decision tree.
- Inner nodes are the comparisons
- The leaves are a permutation of the data that is in order.
- To simplify the math a bit, we will assume that no two data elements are the same
- This is to make each permutation unique.
- We know that for a set of n elements, there are n! permutations.
- Or, in other words, there are n! different ways to "sort" a set of unique elements.
- But only one is "correct".
- So for a comparison based sort of n elements , a decision tree must support n! leaves.
- Therefore the height of the decision tree must be at least height h such that n! ≤ 2h.
- or log2n! ≤ h
- But we want to know this in terms of n, not h.
- Sterling's approximation gives us the relationship
- n! ≈ sqrt(2πn) nn/en
- n! ≈ (2πn)1/2 nne-n
- log2n! ≈ log2(2π n)1/2 nne-n)
- log(ab) = log(a) + log(b)
- log2n! ≈ log2(2πn)1/2 + log2 nn - loge-n
- sqrt(ab) = sqrt(a)sqrt(b), so sqrt(2πn) = sqrt(2π) sqrt(n)
- let sqrt(2π) = c
- sqrt(n) = n1/2
- log2n1/2 = 1/2 log2n
- So log2(n!) ≈ c 1/2 log2n + n log2n - n log2 e
- log2n! ≈ n log2n - n log2e ++ O(log2n)
- Or log2n! ≈ O(n log2 n )
- So O(n log2n) ≤ h, the height of the tree.
- Thus there must be at least O(n log2 n) comparisons in any decision tree that finds the correct permutation.
- And the best ANY comparison based sort can be is O(n log2n)