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

From slowest to fastest growing
ClassNameTypical sourceRough limit at ~10⁸ steps/s
O(1)constantArray index, hash lookup (expected), push onto a stackany n
O(log n)logarithmicBinary search, one heap insert, a balanced-tree lookupany n
O(√n)square rootTrial division up to √nn ≈ 10¹⁴ or more
O(n)linearOne pass over the input, linear searchn ≈ 10⁷ – 10⁸
O(n log n)linearithmicMerge sort, heap sort, sorting with a built-in sortn ≈ 10⁶ – 10⁷
O(n²)quadraticAll pairs, insertion sort on reversed inputn ≈ 10³ – 10⁴
O(n³)cubicThree nested loops, naive matrix multiplicationn ≈ 200 – 500
O(2ⁿ)exponentialEvery subset, naive recursive Fibonacci (Θ(φⁿ) ⊂ O(2ⁿ))n ≈ 20 – 25
O(n!)factorialEvery permutationn ≈ 10 – 11
Limits assume small constant factors. Memory access patterns, allocation and language overhead can move them by an order of magnitude.

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 (j from i to n) 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.
Recurrences that come up again and again
RecurrenceSolutionExample
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

Time per operation (n elements)
StructureAccess by indexSearch by valueInsertDeleteNotes
Array (fixed size)O(1)O(n)O(n) (shift)O(n) (shift)No growth; binary search is O(log n) if sorted
Dynamic arrayO(1)O(n)O(1) amortized at the end, O(n) elsewhereO(1) at the end, O(n) elsewhereGrowth copies everything; capacity grows geometrically
Singly linked listO(n)O(n)O(1) at the head or after a known nodeO(1) at the head; O(n) to find the node firstPoor cache locality
Hash map / set—O(1) expected, O(n) worstO(1) expected (amortized over resizes)O(1) expectedNeeds 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 topPeek 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 LO(L)O(L)Independent of how many keys are stored
Per-language numbers for real built-in collections are in the collections cheatsheet and the collection encyclopedia.

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.

References