Skip to main content

Programming concepts

What is Big O Notation?

Definition

Big O notation describes how an algorithm's running time or memory use grows as its input grows, ignoring constant factors and lower-order terms.

Also known as: Big-O, Asymptotic complexity

Big O Notation in more detail

The question Big O answers is not how fast something is but how it scales. O(n) means doubling the input roughly doubles the work; O(n^2) means doubling it quadruples the work; O(log n) means doubling the input adds one step. Constants and smaller terms are dropped because they stop mattering as n grows: 3n^2 + 100n + 7 is simply O(n^2), since the squared term dominates everything else eventually.

The common classes, best to worst: O(1) constant, such as a hash map lookup; O(log n) logarithmic, such as binary search; O(n) linear, a single pass; O(n log n), which is the bound for comparison sorting; O(n^2) quadratic, typically a nested loop over the same collection; O(2^n) exponential and O(n!) factorial, which are usable only on tiny inputs. Space complexity is measured the same way, and the two often trade against each other — memoisation buys time with memory.

Strictly, O is an upper bound; Θ describes a tight bound and Ω a lower one, and most people say Big O while meaning Θ of the worst case. Two caveats keep it honest in practice. The case matters — quicksort is O(n log n) on average and O(n^2) in the worst case, while hash lookups are O(1) amortised, not always. And constants that Big O discards are real: for small n an O(n^2) algorithm with a tight memory-friendly loop routinely beats an O(n log n) one, which is why library sorts switch to insertion sort on small partitions.

Big O Notation example

n = 1,000,000

  O(1)        1 step               hash map lookup
  O(log n)    ~20 steps            binary search
  O(n)        1,000,000            one pass over the data
  O(n log n)  ~20,000,000          efficient sorting
  O(n^2)      1,000,000,000,000    nested loop  (do not)

Reading code

  for x in items:            O(n)
      print(x)

  for x in items:            O(n^2)  <- nested over the same list
      for y in items:
          compare(x, y)

  seen = set(items)          O(n) once
  if target in seen:         O(1) per check
                             (vs "target in items" -> O(n) each)

Same output, different growth. At n = 10 nobody notices;
at n = 1,000,000 one of them never finishes.

Big O Notation — common questions

What does O(1) mean?
Constant time: the work does not grow with the input size. A hash map lookup or reading an array element by index is O(1) — the same cost whether the collection holds ten items or ten million.
Is O(n log n) good?
It is the practical target for anything that must sort or process every element with some ordering work, and it is the proven lower bound for comparison-based sorting. For most workloads it is close enough to linear to be unremarkable.
Does Big O measure actual speed?
No. It describes growth, not time. An O(n^2) routine can easily beat an O(n log n) one on small inputs because the discarded constants and memory behaviour dominate there — measure before optimising on complexity alone.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary