Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Appendix B: Complexity Cheat Sheet

A comprehensive reference for the time and space complexity of every major data structure, algorithm, and operation.


Notation

SymbolMeaning
O(1)Constant
O(log n)Logarithmic
O(n)Linear
O(n log n)Linearithmic
O(n²)Quadratic
O(n³)Cubic
O(2ⁿ)Exponential
O(n!)Factorial

1. Array Operations

OperationTimeSpaceNotes
Access by indexO(1)O(1)
Search (unsorted)O(n)O(1)Linear scan
Search (sorted)O(log n)O(1)Binary search
Insert at endO(1)*O(1)*Amortized for dynamic arrays
Insert at positionO(n)O(1)Shifts elements
Delete at endO(1)O(1)
Delete at positionO(n)O(1)Shifts elements
CopyO(n)O(n)

2. Linked List Operations

Singly Linked List

OperationTimeSpace
Access by indexO(n)O(1)
SearchO(n)O(1)
Insert at headO(1)O(1)
Insert at tailO(n)O(1)
Insert after nodeO(1)O(1)
Delete headO(1)O(1)
Delete nodeO(n)O(1)
Delete with given nodeO(1)O(1)

Doubly Linked List

OperationTimeSpace
Access by indexO(n)O(1)
SearchO(n)O(1)
Insert at head/tailO(1)O(1)
Insert after/before nodeO(1)O(1)
Delete head/tailO(1)O(1)
Delete nodeO(1)O(1)

3. Stack and Queue

OperationStackQueueDeque
PushO(1)O(1)O(1)
PopO(1)O(1)O(1)
Top/Front/BackO(1)O(1)O(1)
SearchO(n)O(n)O(n)
SizeO(1)O(1)O(1)

4. Hash Table

OperationAverageWorstNotes
InsertO(1)O(n)Worst: all collisions
DeleteO(1)O(n)
SearchO(1)O(n)
IterateO(n)O(n)

5. Binary Heap / Priority Queue

OperationTimeNotes
InsertO(log n)Bubble up
Extract min/maxO(log n)Bubble down
PeekO(1)
Decrease keyO(log n)*With index tracking
Build from arrayO(n)Heapify
Merge two heapsO(n)
Delete arbitraryO(n)O(log n) with index

6. Binary Search Tree (BST)

Balanced BST (AVL, Red-Black, etc.)

OperationAverageWorst (balanced)Worst (unbalanced)
SearchO(log n)O(log n)O(n)
InsertO(log n)O(log n)O(n)
DeleteO(log n)O(log n)O(n)
Min/MaxO(log n)O(log n)O(n)
Successor/PredecessorO(log n)O(log n)O(n)
In-order traversalO(n)O(n)O(n)
K-th smallestO(log n)*O(log n)*O(n)

*Augmented BST with subtree sizes.

C++ std::set / std::map (Red-Black Tree)

OperationTime
InsertO(log n)
EraseO(log n)
FindO(log n)
CountO(log n)
Lower boundO(log n)
Upper boundO(log n)
SizeO(1)
Iteration (next element)O(1) amortized

7. Trie (Prefix Tree)

Let L = length of the string.

OperationTimeSpace
InsertO(L)O(L) per word
SearchO(L)O(1)
Prefix searchO(L)O(1)
DeleteO(L)O(1)
AutocompleteO(L + k)k = number of results

Space: O(ALPHABET_SIZE × N × L) worst case, much less with path compression.


8. Union-Find (Disjoint Set Union)

OperationAmortizedWorst (no optimization)
FindO(α(n)) ≈ O(1)O(n)
UnionO(α(n)) ≈ O(1)O(n)
ConnectedO(α(n)) ≈ O(1)O(n)
Build (n elements)O(n)O(n)

With path compression + union by rank: α(n) is the inverse Ackermann function, effectively ≤ 4 for all practical n.


9. Segment Tree

OperationTimeSpace
BuildO(n)O(n)
Point updateO(log n)O(1)
Range queryO(log n)O(1)
Range update (lazy)O(log n)O(1)
K-th element (with merge sort tree)O(log²n)O(n log n)

10. Fenwick Tree (Binary Indexed Tree)

OperationTimeSpace
BuildO(n log n)O(n)
Point updateO(log n)O(1)
Prefix sum queryO(log n)O(1)
Range update + point queryO(log n)O(1)
Range update + range queryO(log n)O(n)

11. Sparse Table

OperationTimeSpace
BuildO(n log n)O(n log n)
Query (idempotent: min, max, gcd)O(1)
Query (non-idempotent: sum)O(log n)

12. Sorting Algorithms

AlgorithmBestAverageWorstSpaceStableIn-Place
Bubble SortO(n)O(n²)O(n²)O(1)YesYes
Selection SortO(n²)O(n²)O(n²)O(1)NoYes
Insertion SortO(n)O(n²)O(n²)O(1)YesYes
Merge SortO(n log n)O(n log n)O(n log n)O(n)YesNo
Quick SortO(n log n)O(n log n)O(n²)O(log n)NoYes
Heap SortO(n log n)O(n log n)O(n log n)O(1)NoYes
Counting SortO(n + k)O(n + k)O(n + k)O(k)YesNo
Radix SortO(d(n + k))O(d(n + k))O(d(n + k))O(n + k)YesNo
Bucket SortO(n + k)O(n + k)O(n²)O(n + k)YesNo
Tim SortO(n)O(n log n)O(n log n)O(n)YesNo
Intro SortO(n log n)O(n log n)O(n log n)O(log n)NoYes

C++ std::sort: Intro Sort (hybrid of quicksort, heapsort, insertion sort). O(n log n) worst case.


13. Graph Algorithms

Traversal

AlgorithmTimeSpaceNotes
BFSO(V + E)O(V)Queue-based
DFSO(V + E)O(V)Stack/recursion
Iterative DFSO(V + E)O(V)Explicit stack

Shortest Path

AlgorithmTimeSpaceConstraints
Dijkstra (binary heap)O((V + E) log V)O(V)Non-negative weights
Dijkstra (Fibonacci heap)O(V log V + E)O(V)Non-negative weights
Bellman-FordO(VE)O(V)Negative edges OK
SPFA (average)O(E)O(V)Can be O(VE) worst case
Floyd-WarshallO(V³)O(V²)All-pairs
BFS (unweighted)O(V + E)O(V)Unit weights
DAG shortest pathO(V + E)O(V)DAG only
A*O(E) avgO(V)With heuristic

Minimum Spanning Tree

AlgorithmTimeSpaceNotes
KruskalO(E log E)O(V)Edge list + DSU
Prim (binary heap)O((V + E) log V)O(V)Better for dense graphs
Prim (adjacency matrix)O(V²)O(V²)Simple implementation
BorůvkaO(E log V)O(V)Parallel-friendly

Other Graph Algorithms

AlgorithmTimeSpace
Topological SortO(V + E)O(V)
Detect cycle (directed)O(V + E)O(V)
Detect cycle (undirected)O(V + E)O(V)
Strongly Connected Components (Tarjan)O(V + E)O(V)
Strongly Connected Components (Kosaraju)O(V + E)O(V)
Bridge findingO(V + E)O(V)
Articulation point findingO(V + E)O(V)
Eulerian path/circuitO(V + E)O(V)
Bipartite checkO(V + E)O(V)
Max Flow (Edmonds-Karp)O(VE²)O(V²)
Max Flow (Dinic)O(V²E)O(V + E)
Max Flow (Push-Relabel)O(V²√E)O(V²)
Hungarian AlgorithmO(V³)O(V²)
Tree diameterO(V)O(V)
LCA (binary lifting)O(V log V) build, O(log V) queryO(V log V)

14. String Algorithms

Let n = text length, m = pattern length, k = number of matches.

AlgorithmPreprocessingSearchSpace
NaiveO(1)O(nm)O(1)
KMPO(m)O(n + k)O(m)
Z AlgorithmO(n + m)O(n + k)O(n + m)
Rabin-KarpO(m)O(n + m) avg, O(nm) worstO(1)
Aho-CorasickO(m)O(n + k)O(m × ALPHABET)
Boyer-MooreO(m + ALPHABET)O(n/m) best, O(nm) worstO(m + ALPHABET)
Suffix Array (SA-IS)O(n)O(m log n)O(n)
Suffix Array (naive)O(n log²n)O(m log n)O(n)
LCP Array (Kasai)O(n)O(n)
Suffix TreeO(n)O(m + k)O(n)
Manacher’sO(n)O(n)O(n)

15. Dynamic Programming

Problem TypeTimeSpaceExample
1D DPO(n)O(n) or O(1)Fibonacci, climbing stairs
2D DP (n×m grid)O(nm)O(nm) or O(m)LCS, edit distance
Interval DPO(n³)O(n²)Matrix chain, palindrome partition
Bitmask DPO(2ⁿ × n)O(2ⁿ)TSP, subset problems
Digit DPO(digits × states)O(digits × states)Count numbers with property
Tree DPO(n)O(n)Tree diameter, independent set
Knapsack (0/1)O(nW)O(W)
Knapsack (unbounded)O(nW)O(W)
LIS (with binary search)O(n log n)O(n)
LCSO(nm)O(min(n,m))Space-optimized

16. Number Theory

AlgorithmTimeNotes
GCD (Euclidean)O(log(min(a,b)))
LCMO(log(min(a,b)))lcm(a,b) = a/gcd(a,b)*b
Sieve of EratosthenesO(n log log n)
Linear SieveO(n)
Modular exponentiationO(log n)
Modular inverse (Fermat)O(log p)p must be prime
Extended EuclideanO(log(min(a,b)))
Factorization (trial)O(√n)
Factorization (Pollard’s Rho)O(n^(1/4))Expected
Miller-Rabin primalityO(k log²n)k witnesses
Euler’s totientO(√n)
Sieve for Euler’s totientO(n log log n)

17. Geometry

AlgorithmTime
Convex hull (Graham scan)O(n log n)
Convex hull (Andrew’s monotone chain)O(n log n)
Point in polygonO(n)
Closest pair of pointsO(n log n)
Line segment intersectionO(n log n)
Polygon area (Shoelace)O(n)

18. Common STL Operations

std::vector

OperationTime
push_backO(1) amortized
pop_backO(1)
insertO(n)
eraseO(n)
operator[]O(1)
atO(1)
front / backO(1)
size / emptyO(1)
reserveO(n)
resizeO(n)
clearO(n)
shrink_to_fitO(n)

std::set / std::map

OperationTime
insert / emplaceO(log n)
eraseO(log n)
find / count / containsO(log n)
lower_bound / upper_boundO(log n)
size / emptyO(1)
Iteration (next element)O(1) amortized

std::unordered_set / std::unordered_map

OperationAverageWorst
insert / emplaceO(1)O(n)
eraseO(1)O(n)
find / count / containsO(1)O(n)
size / emptyO(1)O(1)
Iteration (next element)O(1)O(1)

std::priority_queue

OperationTime
pushO(log n)
popO(log n)
topO(1)
size / emptyO(1)

std::sort

OperationTime
sortO(n log n)
stable_sortO(n log n)
partial_sortO(n log k)
nth_elementO(n) average
is_sortedO(n)
binary_searchO(log n)
lower_bound / upper_boundO(log n)

19. Space Complexity Summary

Data StructureSpace
ArrayO(n)
Linked ListO(n)
Hash TableO(n)
BSTO(n)
HeapO(n)
TrieO(ALPHABET × n × L)
DSUO(n)
Segment TreeO(n)
Fenwick TreeO(n)
Sparse TableO(n log n)
Adjacency MatrixO(V²)
Adjacency ListO(V + E)
Edge ListO(E)

20. Decision Guide

NeedData StructureComplexity
Fast lookup by keyHash TableO(1) avg
Ordered traversalBST (set/map)O(log n)
Min/Max extractionHeap (priority_queue)O(log n)
Range sum queryFenwick TreeO(log n)
Range min/max querySegment TreeO(log n)
Range update + querySegment Tree + LazyO(log n)
Disjoint setsDSUO(α(n))
String matchingKMP / Z AlgorithmO(n)
Prefix queriesTrieO(L)
Shortest path (non-negative)DijkstraO((V+E)log V)
Shortest path (negative edges)Bellman-FordO(VE)
All-pairs shortest pathFloyd-WarshallO(V³)
MSTKruskal / PrimO(E log V)
Topological orderDFS / BFSO(V + E)
Connected componentsDFS / BFS / DSUO(V + E)

Print this cheat sheet and keep it next to your keyboard during practice. Over time, you’ll memorize it naturally.