Skip to main content

Quick Reference

Big-O Complexity Cheat Sheet

Filter by paradigm or property, sort by complexity, and select up to 3 algorithms to compare their time and space complexities side by side.

Showing 54 of 54 algorithms
Properties:
CompareParadigmResources
Max Pairwise Product (Brute Force)Checks every pair (i, j); baseline for stress testing.
In-placeOptimal
Brute ForceO(n²)O(n²)O(n²)O(1)
Max Pairwise Product (Sorting)Sorts the array first and multiplies the last two elements.
Optimal
Brute ForceO(n log n)O(n log n)O(n log n)O(log n)
Max Pairwise Product (Optimal)Single pass tracking the two largest elements.
In-placeOptimal
Brute ForceO(n)O(n)O(n)O(1)
Sequential SearchWorks on unsorted arrays.
In-placeOptimal
SearchingO(1)O(n)O(n)O(1)
Sequential Search (Sentinel)Eliminates the bounds check inside the loop.
In-placeOptimal
SearchingO(1)O(n)O(n)O(1)
Naive String MatchingBrute-force pattern search of length m in text of length n.
In-placeOptimal
Brute ForceO(n)O(n + m)O(n · m)O(1)
Exhaustive Subset SearchGenerates and tests all 2^n subsets; useful only for small n or as a test oracle.
Optimal
Brute ForceO(2^n · n)O(2^n · n)O(2^n · n)O(n)
Bubble SortO(n) best case with early-exit flag.
StableIn-place
SortingO(n)O(n²)O(n²)O(1)
Bubble Sort (Sentinel)Tracks the last swap index to skip already-sorted suffixes.
StableIn-place
SortingO(n)O(n²)O(n²)O(1)
Bidirectional Bubble SortCocktail shaker sort; mitigates the "turtles" problem.
StableIn-place
SortingO(n)O(n²)O(n²)O(1)
Insertion SortAdaptive; very fast on small or nearly sorted inputs.
StableIn-place
SortingO(n)O(n²)O(n²)O(1)
Binary Insertion SortReduces comparisons to O(n log n), but shifts remain O(n²).
StableIn-place
SortingO(n log n)O(n²)O(n²)O(1)
Selection SortNot stable; performs at most n - 1 swaps.
In-place
SortingO(n²)O(n²)O(n²)O(1)
QuicksortNot stable; median-of-three reduces worst-case risk.
In-place
SortingO(n log n)O(n log n)O(n²)O(log n)
MergesortClassic Divide & Conquer with guaranteed O(n log n).
Stable
SortingO(n log n)O(n log n)O(n log n)O(n)
Binary SearchRequires a sorted array; iterative uses O(1) space.
In-placeOptimal
Divide & ConquerO(1)O(log n)O(log n)O(1)
Vector Summation (D&C)Splits array in half recursively; O(log n) call stack.
Optimal
Divide & ConquerO(n)O(n)O(n)O(log n)
Fibonacci (Naive Recursive)Actually Θ(φ^n); exponential due to overlapping subproblems.
Optimal
Divide & ConquerO(2^n)O(2^n)O(2^n)O(n)
Euclid's GCD AlgorithmComputes gcd(a, b) via repeated modulo reduction.
In-placeOptimal
Divide & ConquerO(1)O(log b)O(log b)O(1)
Towers of HanoiMinimum 2^n - 1 moves; classic subtraction recurrence T(n) = 2T(n-1) + 1.
Optimal
Divide & ConquerO(2^n)O(2^n)O(2^n)O(n)
Karatsuba MultiplicationMultiplies n-digit integers with 3 recursive calls instead of 4.
Optimal
Divide & ConquerO(n^1.585)O(n^1.585)O(n^1.585)O(n)
Tennis Tournament ScheduleBuilds a round-robin schedule for n = 2^k players; optimal for output size.
Optimal
Divide & ConquerO(n²)O(n²)O(n²)O(n²)
Tromino Tiling PuzzleTiles a 2^k × 2^k board (n = 2^k) minus one square in 4 quadrants.
Optimal
Divide & ConquerO(n²)O(n²)O(n²)O(n²)
Majority Element (D&C)Finds an element appearing > n/2 times by halving and counting.
Optimal
Divide & ConquerO(n log n)O(n log n)O(n log n)O(log n)
Mode (Sorting / D&C)Improves over O(n²) brute force by sorting or partitioning.
Optimal
Divide & ConquerO(n log n)O(n log n)O(n log n)O(log n)
Quickselect / MedianFinds the k-th smallest element without sorting the whole array.
In-placeOptimal
Divide & ConquerO(n)O(n)O(n²)O(log n)
Coin Change (Greedy)Optimal only for canonical coin systems (e.g. euros); can fail otherwise.
In-place
GreedyO(n)O(n)O(n)O(1)
Files on a DiskOptimal for maximizing file count (smallest first), not for minimizing free space.
Optimal
GreedyO(n log n)O(n log n)O(n log n)O(1)
Fractional KnapsackOptimal when items can be broken; sort by value/weight ratio.
Optimal
GreedyO(n log n)O(n log n)O(n log n)O(1)
Diligent Plumber SchedulingShortest processing time first minimizes total waiting time.
Optimal
GreedyO(n log n)O(n log n)O(n log n)O(1)
Knight's Tour (Warnsdorff)Heuristic choosing the square with fewest onward moves on an n × n board.GreedyO(n²)O(n²)O(n²)O(n²)
Task Assignment (Greedy)Fast heuristic, but does not guarantee the global minimum cost.GreedyO(n²)O(n²)O(n²)O(n)
Graph Coloring (Greedy)Produces a valid coloring quickly, though not always with minimum colors.GreedyO(V + E)O(V + E)O(V²)O(V)
Prim & Kruskal (MST)Guaranteed optimal Minimum Spanning Tree via priority queue or Union-Find.
Optimal
GreedyO(E log V)O(E log V)O(E log V)O(V + E)
Fibonacci (DP Table)Eliminates redundant calls; space can be reduced to O(1).
Optimal
Dynamic ProgrammingO(n)O(n)O(n)O(n)
Binomial Coefficients (Pascal's Triangle)Avoids exponential recursion by filling Pascal’s triangle bottom-up.
Optimal
Dynamic ProgrammingO(n · k)O(n · k)O(n · k)O(n · k)
0/1 Knapsack (DP)Pseudo-polynomial optimal solution; space can be reduced to O(W).
Optimal
Dynamic ProgrammingO(n · W)O(n · W)O(n · W)O(n · W)
Coin Change (DP)Optimal for arbitrary coin systems where Greedy can fail.
Optimal
Dynamic ProgrammingO(n · A)O(n · A)O(n · A)O(A)
Cheaper Travel on the RiverAll-pairs minimum cost along a DAG of river piers.
Optimal
Dynamic ProgrammingO(n²)O(n²)O(n²)O(n²)
Longest Common Subsequence (LCS)2D table over prefixes of two strings of lengths n and m.
Optimal
Dynamic ProgrammingO(n · m)O(n · m)O(n · m)O(n · m)
Edit Distance (Levenshtein)Minimum insertions, deletions, and substitutions between two strings.
Optimal
Dynamic ProgrammingO(n · m)O(n · m)O(n · m)O(n · m)
Permutations GenerationExplores all n! orderings using a state vector and visited flags.
Optimal
BacktrackingO(n!)O(n!)O(n!)O(n)
Subset Sum (Backtracking)Prunes branches when current sum exceeds target or remaining sum is insufficient.
Optimal
BacktrackingO(n)O(2^n)O(2^n)O(n)
N-Queens (Backtracking)Prunes invalid column/diagonal placements in O(1) per step.
Optimal
BacktrackingO(n!)O(n!)O(n!)O(n)
Knight's Tour (Backtracking)Finds one or all tours; Warnsdorff ordering dramatically speeds up first solution.
Optimal
BacktrackingO(8^(n²))O(8^(n²))O(8^(n²))O(n²)
Graph Coloring (Backtracking)Finds exact k-coloring or chromatic number with m available colors.
Optimal
BacktrackingO(m^V)O(m^V)O(m^V)O(V)
Task Assignment (Backtracking)Exact minimum-cost assignment with bound pruning.
Optimal
BacktrackingO(n!)O(n!)O(n!)O(n)
Sudoku Solver (Backtracking)k empty cells; row/column/box constraints prune almost all invalid branches.
Optimal
BacktrackingO(9^k)O(9^k)O(9^k)O(k)
Task Assignment (B&B)Best-first search using row-minimum lower bounds.
Optimal
Branch & BoundO(n²)O(2^n)O(n!)O(n!)
0/1 Knapsack (B&B)Uses fractional-knapsack upper bound to prune non-promising subtrees.
Optimal
Branch & BoundO(n log n)O(2^n)O(2^n)O(2^n)
8/15-Puzzle (B&B / A*)Uses admissible Manhattan-distance heuristic g + h; checks parity reachability.
Optimal
Branch & BoundO(d)O(b^d)O(b^d)O(b^d)
Traveling Salesman (B&B)Priority-queue search guided by lower-bound cost estimates.
Optimal
Branch & BoundO(n²)O(2^n · n)O(n!)O(n!)
Parallel Array Sum (Fork/Join)Work W(n) = O(n), Span D(n) = O(log n) using sequential threshold.
Optimal
ParallelO(n / p + log n)O(n / p + log n)O(n / p + log n)O(log n)
Parallel Quicksort (Fork/Join)Forks subarray sorting above threshold; span limited by sequential partition.
In-place
ParallelO((n log n) / p)O((n log n) / p)O(n²)O(log n)