Skip to content

Big-O Complexity Reference (1 page)

Common Time Complexities (fastest to slowest)

NotationNameExamplen=10^6 ops
O(1)ConstantHash lookup, array index1
O(log n)LogarithmicBinary search20
O(sqrt(n))Square rootTrial division1000
O(n)LinearLinear scan, counting sort10^6
O(n log n)LinearithmicMerge sort, heap sort2x10^7
O(n^2)QuadraticNested loops, bubble sort10^12 BAD
O(n^3)CubicFloyd-Warshall, matrix mult10^18 BAD
O(2^n)ExponentialSubsets, brute forceheat death
O(n!)FactorialPermutationsheat death

Rule of thumb: ~10^8 simple operations per second in Python. For n=10^6, need O(n log n) or better.

n limitTarget complexity
n <= 10O(n!) or O(2^n)
n <= 20O(2^n)
n <= 25O(2^(n/2))
n <= 100O(n^3)
n <= 1000O(n^2)
n <= 10^5O(n log n)
n <= 10^6O(n) or O(n log n)
n <= 10^8O(n)
n > 10^8O(log n) or O(1)

Python Built-in Operation Costs

list

OperationTime
a[i], a[i]=x, len, append, pop()O(1)
pop(i), insert(i,x), del a[i], x in a, indexO(n)
sortO(n log n)
a[i:j], extend(k items)O(j-i), O(k)
a + bO(n+m)

dict / set

OperationAverage
get, set, del, in, add, removeO(1)
union, intersection, differenceO(n+m), O(min), O(n)
iterationO(n)

str

OperationTime
s[i], lenO(1)
s + t, s[i:j], in, find, replace, split, joinO(n)
Danger: s += c in loop = O(n^2) total. Use ''.join(parts)

deque

OperationTime
append, appendleft, pop, popleftO(1)
a[i] (random access)O(n)

Sorting Algorithm Comparison

AlgorithmBestAverageWorstSpaceStableNotes
Timsort (Python)O(n)O(n log n)O(n log n)O(n)YesPython's sorted()/list.sort()
Merge SortO(n log n)O(n log n)O(n log n)O(n)YesDivide & conquer
Quick SortO(n log n)O(n log n)O(n^2)O(log n)NoRandomized pivot avoids worst
Heap SortO(n log n)O(n log n)O(n log n)O(1)NoIn-place but not cache-friendly
Counting SortO(n+k)O(n+k)O(n+k)O(k)Yesk = range of values
Radix SortO(d(n+k))O(d(n+k))O(d(n+k))O(n+k)Yesd = digits, k = base
Bucket SortO(n+k)O(n+k)O(n^2)O(n+k)YesUniform distribution

Graph Algorithm Complexities

AlgorithmTimeSpaceNotes
BFS / DFSO(V+E)O(V)Adj list; O(V^2) with adj matrix
Topological SortO(V+E)O(V)DAG only
Dijkstra (binary heap)O((V+E) log V)O(V)Non-negative weights
Dijkstra (Fibonacci heap)O(E + V log V)O(V)Theoretical, rarely used
Bellman-FordO(VE)O(V)Handles negative weights
Floyd-WarshallO(V^3)O(V^2)All pairs
Kruskal's MSTO(E log E)O(V)With Union-Find
Prim's MSTO((V+E) log V)O(V)With binary heap
Union-Find (ops)O(alpha(n)) ~ O(1)O(n)Per operation, amortized

Space Complexity Rules of Thumb

Structure / OperationSpace
Recursion depth dO(d) stack frames
DFS on treeO(h) where h = height (O(log n) balanced, O(n) skewed)
BFS on treeO(w) where w = max width (up to O(n/2) = O(n))
BFS on graphO(V) for visited set + queue
Hash map of n itemsO(n)
2D DP table n x mO(nm) -- optimize to O(min(n,m)) with rolling array
Adjacency listO(V+E)
Adjacency matrixO(V^2)
Sorting (Timsort)O(n) auxiliary
HeapO(n) to store, O(1) auxiliary for operations
Trie of k words, avg len LO(k * L) worst case

Recursion Stack Depth

  • Python default recursion limit: 1000
  • Increase with: import sys; sys.setrecursionlimit(10**6)
  • Better: convert to iterative with explicit stack for deep recursion

Common Space Optimizations

  • DP rolling array: O(nm) -> O(m) when row i depends only on row i-1
  • Bit manipulation: Use int as bitset (Python ints are arbitrary precision)
  • In-place modification: Mark visited in grid with sentinel value
  • Generator/iterator: Process stream without storing all in memory
This page lives in git. Anyone can propose an edit. Edit this page View source

A reading surface for the DSA study packet. Content is tracked in git; every page links to its source.