Big O Cheat Sheet: Time and Space Complexity Table

Best, average and worst-case time complexity and space complexity for 92 algorithms and data structures, from sorting and searching to graphs and dynamic programming. Every row links to a visualizer that runs the algorithm step by step.

Ω is the best case, Θ the average case and O the worst case. For data structures, time is the cost of the main operation (search, insert or delete) and space is the memory the structure holds. New to the notation? Start with Big O notation.

Common complexities, fastest to slowest

Big ONameTypical example
O(1)ConstantArray index, hash map lookup, stack push and pop
O(log n)LogarithmicBinary search, balanced BST operations, heap push and pop
O(n)LinearOne pass over the input: linear search, two pointers, sliding window
O(n log n)LinearithmicMerge sort, heap sort, sorting before a sweep
O(n²)QuadraticNested loops over the input: bubble, insertion and selection sort
O(2ⁿ)ExponentialAll subsets: naive recursion, backtracking without pruning
O(n!)FactorialAll orderings: permutations, brute-force travelling salesman

Mathematics: time and space complexity

All mathematics visualizers
AlgorithmBestAverageWorstSpace
GCD / LCMΩ(1)Θ(log(min(A, B)))O(log(min(A, B)))O(1)
Prime NumbersΩ(1)Θ(√n)O(√n)O(1)
Sieve of EratosthenesΩ(n log log n)Θ(n log log n)O(n log log n)O(n)
Modular ArithmeticΩ(1)Θ(1)O(1)O(1)
Fast ExponentiationΩ(log B)Θ(log B)O(log B)O(1)
Bit ManipulationΩ(1)Θ(1)O(1)O(1)

Arrays: time and space complexity

All arrays visualizers
AlgorithmBestAverageWorstSpace
Array OperationsΩ(1)Θ(n)O(n)O(n)
Two PointersΩ(1)Θ(n)O(n)O(1)
Sliding WindowΩ(n)Θ(n)O(n)O(1)
Prefix SumΩ(1)Θ(1)O(1)O(n)
Kadane's AlgorithmΩ(n)Θ(n)O(n)O(1)
Boyer-Moore VotingΩ(n)Θ(n)O(n)O(1)

Strings: time and space complexity

All strings visualizers
AlgorithmBestAverageWorstSpace
Palindrome CheckΩ(1)Θ(n)O(n)O(1)
Anagram CheckΩ(1)Θ(n)O(n)O(K)
Naive SearchΩ(n)Θ(n*M)O(n*M)O(1)
KMP AlgorithmΩ(n + M)Θ(n + M)O(n + M)O(M)
Rabin-KarpΩ(n + M)Θ(n + M)O(n*M)O(1)

Sorting: time and space complexity

All sorting visualizers
AlgorithmBestAverageWorstSpace
Bubble SortΩ(n)Θ(n²)O(n²)O(1)
Selection SortΩ(n²)Θ(n²)O(n²)O(1)
Insertion SortΩ(n)Θ(n²)O(n²)O(1)
Merge SortΩ(n log n)Θ(n log n)O(n log n)O(n)
Quick SortΩ(n log n)Θ(n log n)O(n²)O(log n)
Heap SortΩ(n log n)Θ(n log n)O(n log n)O(1)
Shell SortΩ(n log n)Θ(n^1.5)O(n²)O(1)
Counting SortΩ(n + K)Θ(n + K)O(n + K)O(n + K)
Radix SortΩ(n*k)Θ(n*k)O(n*k)O(n + B)
Bucket SortΩ(n + k)Θ(n + k)O(n²)O(n + k)

Searching: time and space complexity

All searching visualizers
AlgorithmBestAverageWorstSpace
Linear SearchΩ(1)Θ(n)O(n)O(1)
Binary SearchΩ(1)Θ(log n)O(log n)O(1)

Data Structures: time and space complexity

All data structures visualizers
AlgorithmBestAverageWorstSpace
StackΩ(1)Θ(1)O(1)O(n)
QueueΩ(1)Θ(1)O(1)O(n)
DequeΩ(1)Θ(1)O(1)O(n)
Circular QueueΩ(1)Θ(1)O(1)O(n)
Priority Queue (ADT)Ω(1) (Peek)Θ(log n) (Insert/Delete)O(log n) (Insert/Delete)O(n)
Tree TraversalΩ(n)Θ(n)O(n)O(w) or O(h)
Binary TreeΩ(1)Θ(n)O(n)O(n)
Binary Search TreeΩ(log n)Θ(log n)O(n)O(n)
AVL TreeΩ(log n)Θ(log n)O(log n)O(n)
Trie (Prefix Tree)Ω(L)Θ(L)O(L)O(n * L)
Segment TreeΩ(log n)Θ(log n)O(log n)O(n)
Fenwick TreeΩ(log n)Θ(log n)O(log n)O(n)
Red-Black TreeΩ(log n)Θ(log n)O(log n)O(n)
Singly Linked ListΩ(1)Θ(n)O(n)O(n)
Doubly Linked ListΩ(1)Θ(n)O(n)O(n)
Circular Linked ListΩ(1)Θ(n)O(n)O(n)
Graph RepresentationΩ(1)Θ(1)O(V)O(V + E)
Breadth First SearchΩ(V + E)Θ(V + E)O(V + E)O(V)
Depth First SearchΩ(V + E)Θ(V + E)O(V + E)O(V)
Topological SortΩ(V + E)Θ(V + E)O(V + E)O(V + E)
Cycle DetectionΩ(V + E)Θ(V + E)O(V + E)O(V)
Connected ComponentsΩ(V + E)Θ(V + E)O(V + E)O(V)
Bipartite GraphΩ(V + E)Θ(V + E)O(V + E)O(V)
Dijkstra's AlgorithmΩ(E log V)Θ(E log V)O(E log V)O(V + E)
Bellman-Ford AlgorithmΩ(E)Θ(VE)O(VE)O(V)
Minimum Spanning TreePrim: O(E log V) | Kruskal: O(E log E)Prim: O(E log V) | Kruskal: O(E log E)Prim: O(E log V) | Kruskal: O(E log E)O(V + E)
Floyd-Warshall AlgorithmΩ(V³)Θ(V³)O(V³)O(V²) (Matrix)
Bridges & Articulation PointsΩ(V + E)Θ(V + E)O(V + E)O(V) (DFS Stack + Arrays)

Hashing: time and space complexity

All hashing visualizers
AlgorithmBestAverageWorstSpace
Hash MapΩ(1)Θ(1)O(n) (High Collisions)O(n)
Hash SetΩ(1)Θ(1)O(n) (High Collisions)O(n)
Frequency TableΩ(n)Θ(n)O(n)O(K) (Unique Elements)
Perfect HashingΩ(1)Θ(1)O(1)O(n)

Dynamic Programming: time and space complexity

All dynamic programming visualizers
AlgorithmBestAverageWorstSpace
Fibonacci SequenceΩ(n)Θ(n)O(n) (vs O(2^n) for naive recursion)O(n) (Can be optimized to O(1) space)
Climbing StairsΩ(n)Θ(n)O(n)O(n) (Optimizable to O(1))
Unique PathsΩ(m*n)Θ(m*n)O(m*n)O(m*n) (Can be optimized to O(n) or O(min(m,n)))
0/1 KnapsackΩ(n*W)Θ(n*W)O(n*W)O(n*W) (Optimizable to O(W))
Subset SumΩ(n*Target)Θ(n*Target)O(n*Target)O(n*Target) (Optimizable to O(Target))
Longest Common SubsequenceΩ(M*n)Θ(M*n)O(M*n)O(M*n)
Longest Increasing SubsequenceΩ(n²)Θ(n²)O(n²)O(n)
Edit DistanceΩ(M*n)Θ(M*n)O(M*n)O(M*n)
Max Path SumΩ(n)Θ(n)O(n)O(H) (Recursion Stack)
Digit DPΩ(log n)Θ(log n * 10)O(log n * 10 * StateSize)O(log n) (Recursion Depth)

Greedy Algorithms: time and space complexity

All greedy algorithms visualizers
AlgorithmBestAverageWorstSpace
Activity SelectionΩ(n log n) (Sorting)Θ(n log n)O(n log n)O(1) (Auxiliary, excluding sort stack)
Huffman CodingΩ(n log K) (n=len, K=unique chars)Θ(n log K)O(n log K)O(K) (Tree storage)
Interval SchedulingΩ(n log n) (Sorting)Θ(n log n)O(n log n)O(1) (Auxiliary)
Job SequencingΩ(n log n)Θ(n log n + n*D)O(n log n + n*D)O(D)
Minimum PlatformsΩ(n log n) (Sorting)Θ(n log n)O(n log n)O(1) (Auxiliary space for pointers)

Backtracking: time and space complexity

All backtracking visualizers
AlgorithmBestAverageWorstSpace
SubsetsΩ(n * 2^n)Θ(n * 2^n)O(n * 2^n)O(n)
PermutationsΩ(n * n!)Θ(n * n!)O(n * n!)O(n) (Recursion stack)
N-QueensΩ(n!)Θ(n!)O(n!)O(n) (Recursion stack + board storage)
Sudoku SolverΩ(9^M) (M = empty cells)Θ(9^M)O(9^M)O(M) (recursion stack)
Rat in a MazeΩ(3^(n^2))ExponentialO(3^(n^2))O(L * n^2) where L is path length

Advanced Topics: time and space complexity

All advanced topics visualizers
AlgorithmBestAverageWorstSpace
Heap OperationsΩ(1) (Insert if no swim needed)Θ(log n)O(log n)O(n)
Heap SortΩ(n log n)Θ(n log n)O(n log n)O(1)
Optimized Union-FindΩ(α(n))Θ(α(n))O(α(n))O(n)
Kth Largest ElementΩ(n)Θ(n log K)O(n log K)O(K)
Meeting Rooms IIΩ(n log n)Θ(n log n)O(n log n)O(n)
Mo's AlgorithmΩ((n+Q) * sqrt(n))Θ((n+Q) * sqrt(n))O((n+Q) * sqrt(n))O(n + Q)
TriesΩ(1)Θ(L)O(L)O(n*L)
Z-AlgorithmΩ(n)Θ(n)O(n)O(n)
Suffix ArrayΩ(n² log n)Θ(n² log n)O(n² log n)O(n) to O(n²)
Bitmask DPΩ(n * 2^n)Θ(n^2 * 2^n)O(n^2 * 2^n)O(n * 2^n)
Matrix ExponentiationΩ(k^3 log n)Θ(k^3 log n)O(k^3 log n)O(k^2)