Comparison-Based Algorithm

All sorting algorithms we’ve seen so far are comparison based algorithm which involves:

  • comparing two elements
  • moving

Examples of comparison-based algorithm:

Lower Bound for Comparison-Based Sorting Algorithm

Note

Theorem: Any correct comparison-based sorting algorithm requires at least Ω(n log n) comparison operations to sort n distinct items.

Claim: In a binary tree, for , there are at most leaves of level (depth) at most . Proof: if , there is indeed at most leaf of level at most . Otherwise, for , we suppose this is true for , and prove it for . If the tree is just a root, there is leaf of level at most , so we’re good. Else, all the leaves in our tree are either leaves in the left (), or the right subtree () at the root. The leaves of level at most in have level at most in either or , so by the induction assumption there are at most in . This gives a total .

Claim: for any comparison-based sorting algorithm, the worst case number of comparisons for sorting arrays with distinct entries is . Proof: choose and consider the decision tree for our sorting algorithm in size . There are different input (=permutations of an array with distinct elements), so there are leaves that can be reached from these inputs (labelled by sorting permutations).

Remark

There could be more leaves, that cannot be reached from any input; we don’t care about them

Let be the maximal level of these reachable leaves.

Important

This is also the worst case number of comparisons we’re doing in input size n.

All reachable leaves are at levels at most , so by the claim above, there are at most of them. So: . Take a log: , so , and we’re done.

Questions

  • A comparison-based sorting algorithm (that returns the correct result for each instance) must have a best-case runtime that is .
    • False
  • It is not possible for a comparison-based searching algorithm to terminate in