Essential Algorithms and Data Structures Reference

Asymptotic Notation (Growth Rates)

  • O(f(n)) – Upper Bound (Worst Case)
  • Ω(f(n)) – Lower Bound (Best Case)
  • Θ(f(n)) – Tight Bound (Exact)

Growth Rate Hierarchy

O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(2n) < O(n!)

Sorting Algorithms – Time Complexities

AlgorithmBest CaseWorst CaseStableIn-Place
Bubble SortO(n)O(n2)YesYes
Selection SortO(n2)O(n2)NoYes
Insertion SortO(n)O(n2)YesYes
Merge SortO(n log n)O(n log n)YesNo
HeapsortO(n log n)O(n log n)NoYes
QuicksortO(n log n)O(n2)NoYes

Search Methods Complexity

  • Linear Search: O(n) (Unsorted data)
  • Binary Search: O(log n) (Sorted data)
  • Hash Table (Average): O(1) (Unsorted data)

Data Structure Time Complexities

StructureMax Element (Max(S))Membership Check (Member(x))
Ordered ArrayΘ(n)Θ(log n)
Descending ArrayΘ(1)Θ(log n)
Unordered Linked ListΘ(n)Θ(n)
Binary Tree (Height n)Θ(2n)Θ(2n)
Balanced BSTΘ(log n)Θ(log n)
Hash Table (Chaining)Θ(1)*Θ(1)* (*average case)

Master Theorem Summary

Recurrence Relation: T(n) = aT(n/b) + f(n)

  1. Case 1: If f(n) = O(nlogb a – ε), then T(n) = Θ(nlogb a)
  2. Case 2: If f(n) = Θ(nlogb a), then T(n) = Θ(nlogb a log n)
  3. Case 3: If f(n) = Ω(nlogb a + ε), then T(n) = Θ(f(n)) (if regularity holds)

Master Theorem Examples

  • T(n) = 4T(n/2) + n2 → Θ(n2 log n) (Case 2)
  • T(n) = 3T(n/2) + n2 → Θ(n2) (Case 3)
  • T(n) = 2T(n/2) + n log n → Θ(n log2 n) (Case 2)

Heap Operations (Min/Max Heap)

  • Insert: O(log n)
  • Remove Root: O(log n)
  • Build Heap: O(n)
  • Heapsort: O(n log n)

Hashing Methods (Open Addressing)

  • Linear Probing: h(k) + i mod m
  • Quadratic Probing: h(k) + i2 mod m
  • Double Hashing: h1(k) + i * h2(k) mod m
  • Separate Chaining: Linked list per slot

Graph Algorithms Complexity

  • DFS / BFS Traversal: O(V + E)
  • Shortest Path (Unweighted): BFS → O(V + E)
  • Shortest Path (Weighted): Dijkstra → O(E log V)
  • Minimum Spanning Tree (MST): Kruskal / Prim → O(E log V)
  • Cycle Detection: DFS or Union-Find → O(V + E)
  • Topological Sort (DAG): DFS → O(V + E)

NP-Completeness Reductions

Vertex Cover and Independent Set Reductions

  • Vertex Cover ≤P Independent Set: VC size k → IS of size |V| – k
  • Independent Set ≤P Vertex Cover: IS size s → VC of size |V| – s

Common Time Complexities – Quick Reference

  • Binary Search: O(log n)
  • Linear Search: O(n)
  • Build Heap: O(n)
  • Heapsort: O(n log n)
  • Insert in Heap: O(log n)
  • DFS / BFS: O(V + E)
  • Hash Table (Average): O(1)
  • Dijkstra’s (with heap): O(E log V)