{}DSA Atlas

Complexity Cheat Sheet

Operation costs for every major data structure and the time/space of every algorithm on this site, in one place.

Data structure operations

StructureAccessSearchInsertDeleteNotes
Array (static)O(1)O(n)——contiguous, cache-friendly
Dynamic arrayO(1)O(n)O(1)* end, O(n) middleO(1) end, O(n) middle*amortized
Sorted arrayO(1)O(log n)O(n)O(n)binary search
Linked list (singly)O(n)O(n)O(1) at head / after nodeO(1) after node
Doubly linked listO(n)O(n)O(1)O(1) given nodeLRU cache
Stack / queue / dequeO(1) endsO(n)O(1)O(1)
Hash map / set—O(1) avgO(1) avgO(1) avgO(n) worst
Balanced BST (map/set)O(log n)O(log n)O(log n)O(log n)ordered, floor/ceiling
Binary heapO(1) topO(n)O(log n)O(log n) topheapify O(n)
Trie—O(L)O(L)O(L)L = key length
Union-Find—O(α(n))O(α(n)) union—α ≈ constant
Fenwick tree—O(log n) prefixO(log n) update—
Segment tree—O(log n) rangeO(log n) update—lazy for range updates
Sparse table—O(1) range min/max——static only, O(n log n) build

Sorting

AlgorithmBestAverageWorstSpaceStable
Insertionnn²n²1✓
Selectionn²n²n²1✗
Bubblenn²n²1✓
Mergen log nn log nn log nn✓
Quickn log nn log nn²log n✗
Heapn log nn log nn log n1✗
Countingn + kn + kn + kk✓
Radixd(n + b)d(n + b)d(n + b)n + b✓
Timsort (Python, Java objects)nn log nn log nn✓
Introsort (C++ std::sort)n log nn log nn log nlog n✗

Searching and techniques

TechniqueTimeSpace
Linear scanO(n)O(1)
Binary searchO(log n)O(1)
Binary search on answerO(log(range) · check)depends
Two pointersO(n) (plus sort)O(1)
Sliding windowO(n)O(k) for counts
Prefix sumsO(n) build, O(1) queryO(n)
Monotonic stack / dequeO(n)O(n)
Top-k with heapO(n log k)O(k)
QuickselectO(n) avg, O(n²) worstO(1)

Graph algorithms

AlgorithmTimeSpaceUse
BFS / DFSO(V + E)O(V)traversal, unweighted shortest path
Topological sort (Kahn / DFS)O(V + E)O(V)DAG ordering
Dijkstra (binary heap)O((V + E) log V)O(V)non-negative weights
0-1 BFSO(V + E)O(V)weights 0/1
Bellman-FordO(V · E)O(V)negative weights, hop limits
Floyd-WarshallO(V³)O(V²)all pairs
KruskalO(E log E)O(V)MST, sparse
Prim (heap / array)O(E log V) / O(V²)O(V)MST, dense
Tarjan (bridges, SCC)O(V + E)O(V)critical edges, SCCs
Union-FindO(α(n)) per opO(V)dynamic connectivity

Dynamic programming families

FamilyTypical stateTime
1D (robber, stairs, decode)dp[i]O(n)
LISdp[i] or tailsO(n²) / O(n log n)
Grid pathsdp[r][c]O(R·C)
LCS / edit distancedp[i][j]O(m·n)
0/1 & unbounded knapsackdp[w]O(n·W)
Interval DPdp[i][j]O(n³)
Tree DPper nodeO(n)
Bitmask DPdp[mask][i]O(2ⁿ · n²)
Digit DPpos × tight × stateO(digits · 10 · states)

Backtracking / enumeration

EnumerationCountTime with copying
Subsets2ⁿO(n · 2ⁿ)
Permutationsn!O(n · n!)
Combinations C(n, k)C(n, k)O(k · C(n, k))
All submasks of all masks3ⁿO(3ⁿ)

Rough operation budgets (≈10⁸ simple ops/sec)

nMax complexity
10O(n!)
20O(2ⁿ · n)
500O(n³)
5,000O(n²)
10⁶O(n log n)
10⁸O(n)
10¹⁸O(log n)

Useful numbers

ValueApprox
2¹⁰10³
2²⁰10⁶
2³⁰10⁹
2³¹ − 1 (INT_MAX)2.1 × 10⁹
2⁶³ − 1 (LLONG_MAX)9.2 × 10¹⁸
10!3.6 × 10⁶
12!4.8 × 10⁸
20!2.4 × 10¹⁸
log₂(10⁵)≈ 17
log₂(10⁹)≈ 30