Programming

Big-O Complexity Explainer — Time & Space

Understand Big-O by seeing it: exact step counts and running times for O(1) to O(n!) at any input size, a growth chart on linear or log scale, the code patterns behind each class, and a measurement tool that runs your own Python function at growing n and identifies its complexity.
Exact growth figuresMeasure real Python code

Complexity classes

O(n log n) — Linearithmic. Divide in halves, then do linear work at each level.

Typical examples: sorted(), merge sort, heapsort.

def merge_sort(a):
    if len(a) <= 1:
        return a
    mid = len(a) // 2
    left, right = merge_sort(a[:mid]), merge_sort(a[mid:])
    out, i, j = [], 0, 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            out.append(left[i]); i += 1
        else:
            out.append(right[j]); j += 1
    return out + left[i:] + right[j:]

Growth

n = 1,000steps (log scale, up to 2^34)
1,000

Steps and time at n = 1,000

ClassNameStepsTime at 10⁸ steps/s
O(1)Constant110.0 ns
O(log n)Logarithmic1099.7 ns
O(n)Linear1,00010.0 µs
O(n log n)Linearithmic9,96699.7 µs
O(n²)Quadratic1,000,00010.0 ms
O(n³)Cubic1.00 × 10^910.0 seconds
O(2ⁿ)Exponential1.07 × 10^301longer than the universe
O(n!)Factorial4.02 × 10^2567longer than the universe

Logarithms are base 2. Real programs also have constant factors, which Big-O ignores; the times assume one step = 10 nanoseconds.

Measure your code

Define f(n). It is called with n = 1, 2, 3, 4, 8, 16, … and every executed line of your code is counted, up to 400,000 lines per run. Built-in functions count as one step.

Press Measure to run the function with real Python in your browser.

How to use this tool

  1. 1Pick a complexity class to see its code pattern and examples.
  2. 2Move the input size slider to compare step counts and running times.
  3. 3Switch the chart between log and linear scale.
  4. 4Paste a function f(n) under “Measure your code” and run it to estimate its complexity.

Frequently asked questions

Your function f(n) runs with real Python at growing values of n. Every executed line is counted, and the complexity class whose shape best fits those counts is reported.

Values such as 2¹⁰⁰⁰ or 1000! are far beyond normal number ranges, so they are computed with logarithms and shown in scientific notation.

Only how it grows. Constant factors, memory and hardware are ignored; the time column assumes 100 million simple steps per second to give a sense of scale.