Docsity

Big-O Complexity Cheat Sheet – Complete Quick Reference (2026), Cheat Sheet of Algorithms and Programming

Toggle sidebar
bg1
BIG-O QUICK REFERENCE • Print double-sided at 100% scale
BIG-O COMPLEXITY CHEAT SHEET
TIME & SPACE COMPLEXITY • DATA STRUCTURES • SORTING • SEARCHING • GRAPHS
FAST O(1), O(log n) OK O(n), O(n log n) COSTLY O(n²), O(2ⁿ), O(n!)
1. BIG-O BASICS
Big-O gives an asymptotic upper bound on growth as n increases. Formal: f(n)=O(g(n)) when
f(n) ≤ c·g(n) for constants c>0 and n≥n₀.
Growth Name Rule of thumb
O(1) Constant Best; does not scale with n
O(log n) Logarithmic Halving search space
O(√n) Square root Sublinear
O(n) Linear One pass
O(n log n) Linearithmic Efficient general sorting
O(n²) Quadratic Nested scans
O(n³) Cubic Triple nested work
O(2ⁿ) Exponential Subset/state exploration
O(n!) Factorial All permutations
Compare growth rates, not machine speed. Drop constants and lower-order terms: O(3n² + 5n + 9) =
O(n²). Keep separate variables: O(n+m) ≠ O(n).
2. CORE DATA STRUCTURES
Structure Access Search Insert Delete
Array O(1) O(n) O(n) O(n)
Dynamic array O(1) O(n) O(1)* O(n)
Linked list O(n) O(n) O(1)** O(1)**
Stack / Queue O(n) O(n) O(1) O(1)
Deque Ends O(1) O(n) O(1) O(1)
Hash table avg. — O(1) O(1) O(1)
Hash table worst — O(n) O(n) O(n)
Balanced BST O(log n) O(log n) O(log n) O(log n)
Unbalanced BST O(n) O(n) O(n) O(n)
Heap Peek O(1) O(n) O(log n) O(log n)
Trie O(m) O(m) O(m) O(m)
* Dynamic-array append is amortized O(1); resize may cost O(n). ** Linked-list insert/delete is O(1)
once the target node/position is known; finding it costs O(n). Hash worst case: collisions.
3. GRAPH REPRESENTATION
Representation Space Check edge Find neighbors
Adjacency matrix O(V²) O(1) O(V)
Adjacency list O(V+E) O(degree) O(degree)
Dense graph (E≈V²): matrix. Sparse graph (E≈V): adjacency list. V=vertices; E=edges; degree=number
of neighbors.
4. SORTING ALGORITHMS
Sort Best Average Worst Space Stable
Bubble O(n) O(n²) O(n²) O(1) Yes
Selection O(n²) O(n²) O(n²) O(1) No
Insertion O(n) O(n²) O(n²) O(1) Yes
Merge O(nlogn) O(nlogn) O(nlogn) O(n) Yes
Quick O(nlogn) O(nlogn) O(n²) O(logn)* No
Heap O(nlogn) O(nlogn) O(nlogn) O(1) No
Counting O(n+k) O(n+k) O(n+k) O(k) Yes
Radix O(d(n+k)) same same O(n+k) Yes
Bucket O(n+k) O(n+k) O(n²) O(n+k) Usually
TimSort O(n) O(nlogn) O(nlogn) O(n) Yes
k = value range; d = digit count. * Quick Sort stack is O(log n) average, O(n) worst. Stable preserves
the order of equal keys.
WHICH SORT SHOULD YOU CHOOSE?
Scenario Choice
Tiny or nearly sorted input Insertion
General-purpose, stable TimSort / Merge
Low extra memory Heap
Small integer range Counting
Large fixed-width integers Radix
Linked list Merge
5. SEARCHING ALGORITHMS
Search Requirement Average Worst Space
Linear Any O(n) O(n) O(1)
Binary Sorted O(logn) O(logn) O(1) iter.
Ternary Sorted O(logn) O(logn) O(1)
Jump Sorted O(√n) O(√n) O(1)
Interpolation Uniform sorted O(loglogn) O(n) O(1)
Exponential Sorted O(logn) O(logn) O(1)
Hash lookup Hash table O(1) O(n) O(n)
Binary search needs sorted data. If searching repeatedly, sorting first costs O(n log n). Interpolation
search is fast only under suitable distributions.
6. GRAPH ALGORITHMS
Algorithm Purpose Time Space
BFS / DFS Traversal; unweighted
shortest path / cycles
O(V+E) O(V)
Dijkstra Shortest paths; non-
negative weights
O((V+E)logV) O(V)
Bellman-Ford Allows negative edges O(VE) O(V)
Floyd-Warshall All-pairs shortest path O(V³) O(V²)
Kruskal Minimum spanning tree O(ElogE) O(V+E)
Prim Minimum spanning tree O(ElogV)* O(V+E)
Topological sort Order a DAG O(V+E) O(V)
Tarjan / Kosaraju Strong components O(V+E) O(V+E)
Edmonds-Karp Maximum flow O(VE²) O(V+E)
* Common heap implementation. Dijkstra does not support negative edge weights. BFS/DFS
complexity includes both vertices and edges.
pf2

Partial preview of the text

Download Big-O Complexity Cheat Sheet – Complete Quick Reference (2026) and more Cheat Sheet Algorithms and Programming in PDF only on Docsity!

BIG-O QUICK REFERENCE • Print double-sided at 100% scale

BIG-O COMPLEXITY CHEAT SHEET

TIME & SPACE COMPLEXITY • DATA STRUCTURES • SORTING • SEARCHING • GRAPHS

FAST O(1), O(log n) OK O(n), O(n log n) COSTLY O(n²), O(2ⁿ), O(n!)

1. BIG-O BASICS

Big-O gives an asymptotic upper bound on growth as n increases. Formal: f(n)=O(g(n)) when

f(n) ≤ c·g(n) for constants c>0 and n≥n₀.

Growth Name Rule of thumb

O(1) Constant Best; does not scale with n

O(log n) Logarithmic Halving search space

O(√n) Square root Sublinear

O(n) Linear One pass

O(n log n) Linearithmic Efficient general sorting

O(n²) Quadratic Nested scans

O(n³) Cubic Triple nested work

O(2ⁿ) Exponential Subset/state exploration

O(n!) Factorial All permutations

Compare growth rates, not machine speed. Drop constants and lower-order terms: O(3n² + 5n + 9) = O(n²). Keep separate variables: O(n+m) ≠ O(n).

2. CORE DATA STRUCTURES

Structure Access Search Insert Delete

Array O(1) O(n) O(n) O(n) Dynamic array O(1) O(n) O(1)* O(n) Linked list O(n) O(n) O(1)** O(1)** Stack / Queue O(n) O(n) O(1) O(1) Deque Ends O(1) O(n) O(1) O(1) Hash table avg. — O(1) O(1) O(1) Hash table worst — O(n) O(n) O(n) Balanced BST O(log n) O(log n) O(log n) O(log n) Unbalanced BST O(n) O(n) O(n) O(n) Heap Peek O(1) O(n) O(log n) O(log n) Trie O(m) O(m) O(m) O(m)

  • Dynamic-array append is amortized O(1); resize may cost O(n). ** Linked-list insert/delete is O(1) once the target node/position is known; finding it costs O(n). Hash worst case: collisions.

3. GRAPH REPRESENTATION

Representation Space Check edge Find neighbors

Adjacency matrix O(V²) O(1) O(V)

Adjacency list O(V+E) O(degree) O(degree)

Dense graph (E≈V²): matrix. Sparse graph (E≈V): adjacency list. V=vertices; E=edges; degree=number of neighbors.

4. SORTING ALGORITHMS

Sort Best Average Worst Space Stable Bubble O(n) O(n²) O(n²) O(1) Yes Selection O(n²) O(n²) O(n²) O(1) No Insertion O(n) O(n²) O(n²) O(1) Yes Merge O(nlogn) O(nlogn) O(nlogn) O(n) Yes Quick O(nlogn) O(nlogn) O(n²) O(logn)* No Heap O(nlogn) O(nlogn) O(nlogn) O(1) No Counting O(n+k) O(n+k) O(n+k) O(k) Yes Radix O(d(n+k)) same same O(n+k) Yes Bucket O(n+k) O(n+k) O(n²) O(n+k) Usually TimSort O(n) O(nlogn) O(nlogn) O(n) Yes k = value range; d = digit count. * Quick Sort stack is O(log n) average, O(n) worst. Stable preserves the order of equal keys.

WHICH SORT SHOULD YOU CHOOSE?

Scenario Choice

Tiny or nearly sorted input Insertion

General-purpose, stable TimSort / Merge

Low extra memory Heap

Small integer range Counting

Large fixed-width integers Radix

Linked list Merge

5. SEARCHING ALGORITHMS

Search Requirement Average Worst Space Linear Any O(n) O(n) O(1) Binary Sorted O(logn) O(logn) O(1) iter. Ternary Sorted O(logn) O(logn) O(1) Jump Sorted O(√n) O(√n) O(1) Interpolation Uniform sorted O(loglogn) O(n) O(1) Exponential Sorted O(logn) O(logn) O(1) Hash lookup Hash table O(1) O(n) O(n) Binary search needs sorted data. If searching repeatedly, sorting first costs O(n log n). Interpolation search is fast only under suitable distributions.

6. GRAPH ALGORITHMS

Algorithm Purpose Time Space BFS / DFS Traversal; unweighted shortest path / cycles

O(V+E) O(V)

Dijkstra Shortest paths; non- negative weights O((V+E)logV) O(V) Bellman-Ford Allows negative edges O(VE) O(V) Floyd-Warshall All-pairs shortest path O(V³) O(V²) Kruskal Minimum spanning tree O(ElogE) O(V+E) Prim Minimum spanning tree O(ElogV)* O(V+E) Topological sort Order a DAG O(V+E) O(V) Tarjan / Kosaraju Strong components O(V+E) O(V+E) Edmonds-Karp Maximum flow O(VE²) O(V+E)

  • Common heap implementation. Dijkstra does not support negative edge weights. BFS/DFS complexity includes both vertices and edges.

BIG-O QUICK REFERENCE • Print double-sided at 100% scale BIG-O COMPLEXITY CHEAT SHEET | BACK SIDE

DYNAMIC PROGRAMMING • RECURRENCES • SPACE • INTERVIEW QUICK REFERENCE

7. STRING ALGORITHMS

Algorithm Use Time Space Naive match Substring O(nm) O(1) KMP Substring O(n+m) O(m) Rabin-Karp Hash matching O(n+m) avg; O(nm) worst

O(1)

Boyer-Moore Substring O(n/m) best; O(nm) worst O(m+σ) Z-algorithm Prefix / pattern O(n+m) O(n+m) Manacher's Longest palindrome O(n) O(n) Suffix array Substring queries O(nlogn) build O(n) Suffix tree Substring queries O(n) build O(n) Aho-Corasick Many patterns O(n+m+z) O(mσ) n=text length; m=pattern length; z=number of matches; σ=alphabet size.

8. DYNAMIC PROGRAMMING CLASSICS

Problem Time Space Optimized Fibonacci O(n) O(n) O(1) 0/1 Knapsack O(nW) O(nW) O(W) Unbounded Knapsack O(nW) O(W) O(W) LCS / Edit Distance O(mn) O(mn) O(min(m,n)) LIS basic O(n²) O(n) O(n) LIS + binary search O(nlogn) O(n) O(n) Coin change O(n·amount) O(amount) O(amount) Matrix-chain mult. O(n³) O(n²) O(n²) Floyd-Warshall O(V³) O(V²) O(V²) Subset sum / partition O(nS) O(nS) O(S) House robber O(n) O(n) O(1) Palindromic subseq. O(n²) O(n²) O(n²) Egg dropping O(eggs·floors²) O(eggs·floors) Depends n=items or sequence length; W=capacity; S=target sum; m=second string length. DP commonly trades memory for speed; optimize when only prior states are needed.

9. MASTER THEOREM & RECURRENCES

For T(n)=aT(n/b)+f(n), with a≥1 and b>1: Case Compare f(n) with n^(log_b a) Result 1 Polynomially smaller Θ(n^(log_b a)) 2 Same order Θ(n^(log_b a)·log n) 3 Polynomially larger + regularity Θ(f(n)) Algorithm / recurrence Complexity Binary search: T(n)=T(n/2)+O(1) O(log n) Merge sort: 2T(n/2)+O(n) O(nlogn) Quick sort worst: T(n−1)+O(n) O(n²) Strassen: 7T(n/2)+O(n²) O(n^2.81) Karatsuba: 3T(n/2)+O(n) O(n^1.585) FFT: 2T(n/2)+O(n) O(nlogn) Tower of Hanoi: 2T(n−1)+O(1) O(2ⁿ)

10. BACKTRACKING WORST CASES

Problem Time Space N-Queens O(n!) O(n) Sudoku O(9^(n²)) worst O(n²) Permutations O(n!) O(n) Combinations O(C(n,k)) O(k) Subsets O(2ⁿ) O(n) Graph coloring O(kⁿ) O(n) Hamiltonian path O(n!) O(n) Word search O(r·c·4^L) O(L) Backtracking explores choices and prunes invalid branches. Worst-case time is often exponential even when pruning helps in practice.

11. BIT MANIPULATION

Operation Expression Time Check bit i (x >> i) & 1 O(1) Set bit i x |= (1 << i) O(1) Clear bit i x &= ~(1 << i) O(1) Toggle bit i x ^= (1 << i) O(1) Count set bits while x: x &= x-1 O(set bits) Power of 2 x>0 and !(x&(x-1)) O(1) Lowest set bit x & -x O(1) XOR swap a^=b; b^=a; a^=b O(1)

12. COMMON OPERATION → DATA STRUCTURE

Need Typical choice Complexity Insert at end Dynamic array O(1) amortized Insert at beginning Linked list / deque O(1) Find min/max Heap O(1) peek; O(logn) extract Kth smallest Quickselect / heap O(n) avg / O(nlogk) Membership test Hash set O(1) avg Predecessor / successor Balanced BST O(logn) Range query Segment tree / Fenwick O(logn) Prefix-sum query Prefix array O(1) Deduplicate Hash set O(n) LRU cache Hash map + doubly linked list O(1) avg Median of stream Two heaps O(logn) insert

13. SPACE COMPLEXITY: HIDDEN COSTS

Structure / algorithm Space Note Array / hash table O(n) Hash overhead/load factor Recursion O(depth) Call stack Merge sort O(n) Auxiliary array Quick sort O(logn) avg O(n) stack worst Adjacency matrix O(V²) Dense graph Adjacency list O(V+E) Sparse graph DP table O(states) Often compressible Trie O(total key chars) Can be memory-heavy BFS / DFS O(V) Queue or visited + stack Auxiliary space means extra memory beyond the input. Interviewers often want auxiliary space separately from total space.

14. AMORTIZED ANALYSIS

Operation Amortized Worst Why Dynamic-array push O(1) O(n) Resize Hash insert O(1) O(n) Rehash / collisions Union-Find* O(α(n))≈O(1) — Path compression + rank Splay-tree access O(logn) O(n) Across a sequence α(n), inverse Ackermann, is below 5 for practical input sizes. Amortized O(1) is not the same as worst- case O(1).

15. P, NP, NP-COMPLETE, NP-HARD

Class Meaning Examples P Solvable in polynomial time Sorting, shortest path, MST NP Solution verifiable in polynomial time SAT, TSP decision, clique NP-complete In NP and NP-hard SAT, 3-SAT, graph coloring NP-hard At least as hard as NP- complete; may be outside NP TSP optimization; halting problem P ⊆NP. Whether P = NP is unresolved; most researchers expect P ≠ NP. NP-hard examples include problems not in NP, so not every NP-hard problem is NP-complete.

16. INTERVIEW CUES & COMMON MISTAKES

Cue / mistake Remember O(1) lookup Hash table: average O(1), worst O(n) Need sorted order Balanced BST or sorted array FIFO / LIFO Queue / stack Top K Heap of size K Unweighted shortest path BFS; O(V+E) Weighted shortest path Dijkstra only with non-negative weights Negative edge weights Bellman-Ford Cycle / components DFS or Union-Find, depending on graph Quick sort always O(nlogn) False: worst O(n²) Binary search O(1) False: O(logn); requires sorted input BFS O(V) Incomplete: O(V+E) Merge sort in-place False: usually O(n) extra space Nested loops always O(n²) Only if both scale with n String concatenation O(1) Often O(length) per concat FINAL CHECK: state the input size, give time + auxiliary space, distinguish average from worst case, and mention data assumptions (sorted input, non-negative weights, bounded key range).