Lower limit for sorting algorithms based on comparison
How fast can we sort
Until now, we covered some algorithms which base their decision on the comparisons between keys but we had no assumption regarding the input's nature.
So what is the inferior limit, when it comes to these algorithms?
- A quick and wrong answer would be that the best time would be
, but this is wrong
1 - Decision Tree as a representation
Let us use a decision tree to represent the comparisons done by a comparison sorting algorithm, given a generic input:

We notice that:
- Every internal node is labeled from
, with - It means we need to compare
and - L-subtree, includes the following comparisons if
; - R-subtree, includes the following comparisons if
;
- It means we need to compare
- Every leaf represents a permutation
- Such that
- A permutation such that every element is sorted by some criteria;
- Such that
Given any comparison sorting algorithm, and some input of size
- Given any comparison sorting algorithm we can build a tree for every
- Each tree maps all the possible execution paths
- The execution time
, which equals the count of comparisons, is the length of a path in the tree - The worst execution time is given by the height of the tree
- The worst execution time is given by the height of the tree
The inferior limit on the heights of all the decisions trees (where each permutation appears like a leaf) behaves like the inferior limit of the execution time of any comparison sorting algorithm
Since it is a binary tree, we can use its height to find out the number of permutations which are exactly the number of leaves in the tree!
2 - How many leaves can a decision tree, like the one we just mentioned, have?
2.1 -
Every permutation must appear at least once, trivially
2.2 Lemma: a binary tree of height has at most leaves
Demonstration
Induction on
- Base Case:
, a tree with just one node which is both root and leaf - Inductive Hypothesis: We assume this is true for any tree of height
- Inductive Step: We show it for
- Tree with one child
is equal to which has height , - Then, by Inductive Hypothesis,
- Tree with both children
, the leaves of the tree equals the sum of its children's leaves - Let
be the height of the children - Then, by Inductive Hypothesis,
- The property is demonstrated as
- Tree with one child
3: Theorem
Any comparison sorting algorithm requires
comparisons
Demonstration
We need to determine the height of a binary decision tree where each permutation appears as a leaf. Let us consider a decision tree with height
Stirling's approximation for
If
Corollary
Heapsort and Mergesort are asymptotically efficient comparison sorting algorithms.