Cheatsheet
Big O and the cost of common operations
Growth classes from O(1) to O(n!), rough input-size limits, and what core operations cost on arrays, lists, hash tables, trees and heaps.
Version 1 · updated 2026-11-24
Notation
O(f(n))- Upper bound: for large enough n the cost grows no faster than a constant times f(n).
Ω(f(n))- Lower bound: the cost grows at least as fast as a constant times f(n).
Θ(f(n))- Tight bound: both O and Ω. Most "it is O(n)" statements in practice mean Θ(n).
- Worst / best / average
- Cases are about which input of size n you get. They are independent of O, Ω and Θ: "worst case Θ(n²)" is a valid statement.
- Expected
- Averaged over the algorithm’s own randomness or a random hash function, for every input. Hash table lookups are expected O(1).
- Amortized
- Total cost of any sequence of m operations divided by m. A dynamic array append is O(n) once in a while and amortized O(1).
- Auxiliary space
- Extra memory beyond the input, including the recursion stack.
Growth classes
| Class | Name | Typical source | Rough limit at ~10⁸ steps/s |
|---|---|---|---|
O(1) | constant | Array index, hash lookup (expected), push onto a stack | any n |
O(log n) | logarithmic | Binary search, one heap insert, a balanced-tree lookup | any n |
O(√n) | square root | Trial division up to √n | n ≈ 10¹⁴ or more |
O(n) | linear | One pass over the input, linear search | n ≈ 10⁷ – 10⁸ |
O(n log n) | linearithmic | Merge sort, heap sort, sorting with a built-in sort | n ≈ 10⁶ – 10⁷ |
O(n²) | quadratic | All pairs, insertion sort on reversed input | n ≈ 10³ – 10⁴ |
O(n³) | cubic | Three nested loops, naive matrix multiplication | n ≈ 200 – 500 |
O(2ⁿ) | exponential | Every subset, naive recursive Fibonacci (Θ(φⁿ) ⊂ O(2ⁿ)) | n ≈ 20 – 25 |
O(n!) | factorial | Every permutation | n ≈ 10 – 11 |
Counting rules
- Sequential blocks add: O(f) then O(g) is O(f + g), which is O(max(f, g)).
- Nested loops multiply when the inner bound does not depend on the outer index: O(n · m).
- A triangular inner loop (
jfromiton) still sums to n(n + 1)/2, so Θ(n²). - A loop that halves or doubles its variable runs about log₂ n times.
- Two different inputs keep two variables: O(n + m) is not O(n).
- Drop constants and lower-order terms only at the end: 3n² + 10n + 7 is Θ(n²).
- Recursion: cost = (number of calls) × (work per call), or solve the recurrence.
| Recurrence | Solution | Example |
|---|---|---|
T(n) = T(n/2) + O(1) | Θ(log n) | Binary search |
T(n) = T(n − 1) + O(1) | Θ(n) | Linear recursion over a list |
T(n) = T(n − 1) + O(n) | Θ(n²) | Quicksort with the worst pivot every time |
T(n) = 2T(n/2) + O(n) | Θ(n log n) | Merge sort |
T(n) = 2T(n/2) + O(1) | Θ(n) | Visiting every node of a balanced binary tree |
T(n) = 2T(n − 1) + O(1) | Θ(2ⁿ) | Branching into two subproblems that shrink by one |
Operations on common structures
| Structure | Access by index | Search by value | Insert | Delete | Notes |
|---|---|---|---|---|---|
| Array (fixed size) | O(1) | O(n) | O(n) (shift) | O(n) (shift) | No growth; binary search is O(log n) if sorted |
| Dynamic array | O(1) | O(n) | O(1) amortized at the end, O(n) elsewhere | O(1) at the end, O(n) elsewhere | Growth copies everything; capacity grows geometrically |
| Singly linked list | O(n) | O(n) | O(1) at the head or after a known node | O(1) at the head; O(n) to find the node first | Poor cache locality |
| Hash map / set | — | O(1) expected, O(n) worst | O(1) expected (amortized over resizes) | O(1) expected | Needs a good hash and consistent equality |
| Balanced BST | — | O(log n) | O(log n) | O(log n) | Keeps keys in order: min, max, range queries |
| Unbalanced BST | — | O(h), up to O(n) | O(h) | O(h) | Sorted inserts degrade it into a list |
| Binary heap | — | O(n) | O(log n) | O(log n) for the top | Peek top O(1); build from n items O(n) |
| Stack / queue | — | O(n) | O(1) (amortized when array-backed) | O(1) | Only the ends are accessible |
| Trie | — | O(L) for a key of length L | O(L) | O(L) | Independent of how many keys are stored |
Space
- Recursion depth counts: recursive binary search uses O(log n) stack, a recursive list walk O(n).
- Merge sort on arrays needs an O(n) buffer; heap sort sorts in O(1) extra space.
- An output that is part of the answer (the returned array) is usually reported separately from auxiliary space.
- Memoization trades space for time: one stored result per distinct state.