Contents

Computer Science › Algorithms

Common Complexity Classes

O(1), O(log n), O(n), O(n log n), O(n²), O(2ⁿ) and what they feel like in practice.

Also known as: O(n) classes, growth rates, Big O classes

Big O notation groups algorithms into classes by how their cost grows as the input size n grows. Knowing the common ones tells you what scales and what doesn’t.

ClassNameFeels likeExample
O(1)constantinstant, regardless of sizearray access, dictionary lookup
O(log n)logarithmicbarely growsbinary search
O(n)lineardoubles when n doublesscanning a list
O(n log n)linearithmica bit over lineargood sorts (sorting)
O(n²)quadraticn doubles, work quadruplesnested loops over the same data
O(2ⁿ)exponentialunusable past ~30trying every subset
O(n!)factorialunusable past ~12trying every ordering

What it means with numbers

Roughly how many steps for n items:

nlog nnn log nn²
1031033100
1,000101,00010,0001,000,000
1,000,000201,000,00020,000,00010¹²

At a million items, n² is a trillion steps, which is hours. n log n is a fraction of a second.

Rules of thumb

  • Nested loops over the same data usually mean O(n²).
  • Halving the problem each time gives log n.
  • Loop plus a lookup in a list hides O(n²): for x in a: if x in b where b is a list. Make b a set (hash tables).
  • Drop constants and smaller terms: 3n + 10 is O(n).
  • The classes describe growth, not speed. For small n, an O(n²) algorithm can beat an O(n log n) one.

Exponential and factorial algorithms are the territory of hard problems (NP-complete). See Big O notation.