Sorting

  • Input: Unordered collection of size n
  • Output: Ordered collection of size n
  • Properties
    • Stable sort: the order between equal elements remains after sorting

Basic Sorts

CaseSelectionBubbleInsertion
Worst (reversed) (reversed)
Average
Best (already sorted) (already sorted)
Space

Advanced Sorts

CaseShellMergeQuick
Worst (bad pivots chosen)
Average
Best
Space (from recursion stack)

Funny Sorts

  • Sleep Sort
  • Counting Sort
  • BOGO Sort

Lower Bound

Theorem

Any deterministic compare-based sorting algorithm must make compares in the worst case

Proof:

  • Assume array consists distinct values through .
  • Worst-case number of compares = height of comparison tree.
  • Binary tree of height has leaves.
  • different orderings reachable leaves.
  • So (using sterling’s formula):