Skip to content
SimpleScope

Big-O notation explained

Big-O notation describes how the work of an algorithm grows as its input grows. It ignores constants and small terms and keeps only the part that dominates for large inputs, so it tells you which algorithm will keep up when the data gets big.

See the growth

Move the slider. The curves that leave the chart are the ones that stop being practical.

  • O(1)Constant
  • O(log n)Logarithmic
  • O(n)Linear
  • O(n log n)Linearithmic
  • O(n²)Quadratic
  • O(2ⁿ)Exponential
Operations needed for n = 16
GrowthOperations for n = 16
O(1) Constant1
O(log n) Logarithmic4
O(n) Linear16
O(n log n) Linearithmic64
O(n²) Quadratic256
O(2ⁿ) Exponential65,536

What it means in time

Assuming a computer that does a billion simple operations per second. Real programs are slower, but the proportions are the same.

Growthn = 1,000n = 1,000,000
O(log n)under a millisecondunder a millisecond
O(n)under a millisecond1 milliseconds
O(n log n)under a millisecond20 milliseconds
O(n²)1 milliseconds17 minutes

Where each one shows up

Rules of thumb

  • Drop constants. Doing twice the work is still linear: O(2n) is O(n).
  • Keep the dominant term. n² + n is O(n²), because n² wins for large n.
  • Nested loops multiply. A loop over n inside a loop over n is O(n²).
  • Halving means a logarithm. Cutting the problem in half each step takes about log n steps.
  • Say which case you mean. Best, average and worst can differ a lot: quick sort is O(n log n) on average and O(n²) in the worst case.

The algorithms in the path

AlgorithmBestAverageWorstSpace
Linear searchO(1)O(n)O(n)O(1)
Binary searchO(1)O(log n)O(log n)O(1)
Bubble sortO(n)O(n²)O(n²)O(1)
Selection sortO(n²)O(n²)O(n²)O(1)
Insertion sortO(n)O(n²)O(n²)O(1)
Merge sortO(n log n)O(n log n)O(n log n)O(n)
Quick sortO(n log n)O(n log n)O(n²)O(log n)
Breadth-first searchO(V + E)O(V + E)O(V + E)O(V)
Depth-first searchO(V + E)O(V + E)O(V + E)O(V)
Dijkstra's algorithmO(V²)O(V²)O(V²)O(V)
StackO(1)O(1)O(1)O(n)
QueueO(1)O(1)O(1)O(n)
Binary search treeO(1)O(log n)O(n)O(n)
Binary heapO(1)O(log n)O(log n)O(n)
Linked listO(1)O(n)O(n)O(n)
Hash tableO(1)O(1)O(n)O(n)
Heap sortO(n log n)O(n log n)O(n log n)O(1)
Counting sortO(n + k)O(n + k)O(n + k)O(n + k)
Radix sortO(d (n + k))O(d (n + k))O(d (n + k))O(n + k)

V is the number of nodes and E the number of edges. Dijkstra is shown with the simple list used in its lesson; a heap makes it O((V + E) log V). For counting and radix sort, k is the number of possible values (or digit values) and d the number of digits; they compare no values.