Data Structures & Algorithms · Revision notes

Searching & Sorting Algorithms

Binary search halves a sorted search space in O(log n); sorting algorithms trade time, space and stability: O(n^2) elementary sorts, O(n log n) merge sort (stable, O(n) space) and quick sort (in-place, O(n^2) worst case).

Quick revision

  • Binary search: mid = l + (r-l)//2; lower_bound; search on monotone answer
  • Insertion O(n)/O(n^2), stable, adaptive; selection <= n-1 swaps, unstable
  • Merge: Theta(n log n) always, O(n) space, stable; best for linked lists
  • Quick: avg n log n, worst n^2, in-place, unstable; random pivot; 3-way for duplicates
  • Comparison sort lower bound: Omega(n log n)
Read the full Searching & Sorting Algorithms notes on Doubtclip →