Skip to content
Definition
Big O notation describes the upper bound (worst-case) growth rate of an algorithm's time or space complexity as the input size increases. It provides a standardised way to compare algorithm efficiency.

Common Big O Complexities

Key Point
Big ONameTypical exampleGrowth intuition
O(1)ConstantArray lookup by indexCost stays about the same as n grows
O(log n)LogarithmicBinary searchDoubling n adds only a small extra cost
O(n)LinearLinear searchCost grows in direct proportion to n
O(n log n)Log-linearMerge sort, quicksort (average)Efficient for large sorting tasks
O(n²)QuadraticBubble sort, insertion sort (worst/avg)Cost grows rapidly as n increases
O(2ⁿ)ExponentialBrute-force recursionExplodes quickly; impractical at scale

Time vs Space Complexity

Key Point
Complexity typeWhat it measuresTypical trade-off
Time complexityHow runtime grows with input size nFaster runtime may need extra memory
Space complexityHow extra memory use grows with nLower memory usage may increase runtime

Working Out the Complexity of an Algorithm

Definition
The complexity of an algorithm is found by working out how the number of steps it takes, or the memory it needs, grows as the number of items, n, increases. Constants and smaller terms are ignored, so only the fastest-growing term is kept: an algorithm that takes 3n + 5 steps is O(n).
What the algorithm doesComplexityReason
A fixed number of steps with no loop over the dataO(1), constantThe number of steps does not depend on n
Halves the data still to be dealt with on each passO(log n), logarithmicDoubling n adds only one more pass
One loop that visits each item onceO(n), linearn items need n passes
One loop over the data nested inside anotherO(n²), polynomialThe inner loop makes n passes for each of the n passes of the outer loop
Tries every combination, so each extra item doubles the workO(2ⁿ), exponentialThe number of steps doubles for every item added
Worked Example
Finding the Big O of a Nested Loop
pseudocode
for i = 0 to n - 1:
    for j = 0 to n - 1:
        if i != j AND items[i] == items[j] then
            print("Duplicate found")
        end if
    end for
end for
The inner loop runs n times for each of the n passes of the outer loop, so the comparison is made n × n times. The time complexity is O(n²), which is polynomial. The algorithm needs only the two counters however large n is, so its space complexity is O(1).
Key Point
Polynomial complexity means n raised to a fixed power, such as O(n²) or O(n³). O(n²) is also called quadratic.

Best, average and worst case can differ for the same algorithm. A linear search is O(1) in the best case, when the target is the first item, and O(n) in the worst case, when the target is last or not in the list.