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:
| Compare | Paradigm | Resources | |||||
|---|---|---|---|---|---|---|---|
| Max Pairwise Product (Brute Force)Checks every pair (i, j); baseline for stress testing. In-placeOptimal | Brute Force | O(n²) | O(n²) | O(n²) | O(1) | ||
| Max Pairwise Product (Sorting)Sorts the array first and multiplies the last two elements. Optimal | Brute Force | O(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 Force | O(n) | O(n) | O(n) | O(1) | ||
| Sequential SearchWorks on unsorted arrays. In-placeOptimal | Searching | O(1) | O(n) | O(n) | O(1) | ||
| Sequential Search (Sentinel)Eliminates the bounds check inside the loop. In-placeOptimal | Searching | O(1) | O(n) | O(n) | O(1) | ||
| Naive String MatchingBrute-force pattern search of length m in text of length n. In-placeOptimal | Brute Force | O(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 Force | O(2^n · n) | O(2^n · n) | O(2^n · n) | O(n) | ||
| Bubble SortO(n) best case with early-exit flag. StableIn-place | Sorting | O(n) | O(n²) | O(n²) | O(1) | ||
| Bubble Sort (Sentinel)Tracks the last swap index to skip already-sorted suffixes. StableIn-place | Sorting | O(n) | O(n²) | O(n²) | O(1) | ||
| Bidirectional Bubble SortCocktail shaker sort; mitigates the "turtles" problem. StableIn-place | Sorting | O(n) | O(n²) | O(n²) | O(1) | ||
| Insertion SortAdaptive; very fast on small or nearly sorted inputs. StableIn-place | Sorting | O(n) | O(n²) | O(n²) | O(1) | ||
| Binary Insertion SortReduces comparisons to O(n log n), but shifts remain O(n²). StableIn-place | Sorting | O(n log n) | O(n²) | O(n²) | O(1) | ||
| Selection SortNot stable; performs at most n - 1 swaps. In-place | Sorting | O(n²) | O(n²) | O(n²) | O(1) | ||
| QuicksortNot stable; median-of-three reduces worst-case risk. In-place | Sorting | O(n log n) | O(n log n) | O(n²) | O(log n) | ||
| MergesortClassic Divide & Conquer with guaranteed O(n log n). Stable | Sorting | O(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 & Conquer | O(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 & Conquer | O(n) | O(n) | O(n) | O(log n) | ||
| Fibonacci (Naive Recursive)Actually Θ(φ^n); exponential due to overlapping subproblems. Optimal | Divide & Conquer | O(2^n) | O(2^n) | O(2^n) | O(n) | ||
| Euclid's GCD AlgorithmComputes gcd(a, b) via repeated modulo reduction. In-placeOptimal | Divide & Conquer | O(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 & Conquer | O(2^n) | O(2^n) | O(2^n) | O(n) | ||
| Karatsuba MultiplicationMultiplies n-digit integers with 3 recursive calls instead of 4. Optimal | Divide & Conquer | O(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 & Conquer | O(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 & Conquer | O(n²) | O(n²) | O(n²) | O(n²) | ||
| Majority Element (D&C)Finds an element appearing > n/2 times by halving and counting. Optimal | Divide & Conquer | O(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 & Conquer | O(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 & Conquer | O(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 | Greedy | O(n) | O(n) | O(n) | O(1) | ||
| Files on a DiskOptimal for maximizing file count (smallest first), not for minimizing free space. Optimal | Greedy | O(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 | Greedy | O(n log n) | O(n log n) | O(n log n) | O(1) | ||
| Diligent Plumber SchedulingShortest processing time first minimizes total waiting time. Optimal | Greedy | O(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. | Greedy | O(n²) | O(n²) | O(n²) | O(n²) | ||
| Task Assignment (Greedy)Fast heuristic, but does not guarantee the global minimum cost. | Greedy | O(n²) | O(n²) | O(n²) | O(n) | ||
| Graph Coloring (Greedy)Produces a valid coloring quickly, though not always with minimum colors. | Greedy | O(V + E) | O(V + E) | O(V²) | O(V) | ||
| Prim & Kruskal (MST)Guaranteed optimal Minimum Spanning Tree via priority queue or Union-Find. Optimal | Greedy | O(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 Programming | O(n) | O(n) | O(n) | O(n) | ||
| Binomial Coefficients (Pascal's Triangle)Avoids exponential recursion by filling Pascal’s triangle bottom-up. Optimal | Dynamic Programming | O(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 Programming | O(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 Programming | O(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 Programming | O(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 Programming | O(n · m) | O(n · m) | O(n · m) | O(n · m) | ||
| Edit Distance (Levenshtein)Minimum insertions, deletions, and substitutions between two strings. Optimal | Dynamic Programming | O(n · m) | O(n · m) | O(n · m) | O(n · m) | ||
| Permutations GenerationExplores all n! orderings using a state vector and visited flags. Optimal | Backtracking | O(n!) | O(n!) | O(n!) | O(n) | ||
| Subset Sum (Backtracking)Prunes branches when current sum exceeds target or remaining sum is insufficient. Optimal | Backtracking | O(n) | O(2^n) | O(2^n) | O(n) | ||
| N-Queens (Backtracking)Prunes invalid column/diagonal placements in O(1) per step. Optimal | Backtracking | O(n!) | O(n!) | O(n!) | O(n) | ||
| Knight's Tour (Backtracking)Finds one or all tours; Warnsdorff ordering dramatically speeds up first solution. Optimal | Backtracking | O(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 | Backtracking | O(m^V) | O(m^V) | O(m^V) | O(V) | ||
| Task Assignment (Backtracking)Exact minimum-cost assignment with bound pruning. Optimal | Backtracking | O(n!) | O(n!) | O(n!) | O(n) | ||
| Sudoku Solver (Backtracking)k empty cells; row/column/box constraints prune almost all invalid branches. Optimal | Backtracking | O(9^k) | O(9^k) | O(9^k) | O(k) | ||
| Task Assignment (B&B)Best-first search using row-minimum lower bounds. Optimal | Branch & Bound | O(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 & Bound | O(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 & Bound | O(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 & Bound | O(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 | Parallel | O(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 | Parallel | O((n log n) / p) | O((n log n) / p) | O(n²) | O(log n) |