Programming
Big-O Complexity Explainer — Time & Space
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
Steps and time at n = 1,000
| Class | Name | Steps | Time at 10⁸ steps/s |
|---|---|---|---|
| O(1) | Constant | 1 | 10.0 ns |
| O(log n) | Logarithmic | 10 | 99.7 ns |
| O(n) | Linear | 1,000 | 10.0 µs |
| O(n log n) | Linearithmic | 9,966 | 99.7 µs |
| O(n²) | Quadratic | 1,000,000 | 10.0 ms |
| O(n³) | Cubic | 1.00 × 10^9 | 10.0 seconds |
| O(2ⁿ) | Exponential | 1.07 × 10^301 | longer than the universe |
| O(n!) | Factorial | 4.02 × 10^2567 | longer 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
- 1Pick a complexity class to see its code pattern and examples.
- 2Move the input size slider to compare step counts and running times.
- 3Switch the chart between log and linear scale.
- 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.
Related tools
Algorithm Visualizer — Sort, Search & More
Step through classic algorithms — bubble sort, binary search, linear search, factorial — with live variable inspection.
Python Data Structure Explorer — Lists, Dicts, Sets
Explore list, tuple, set, dict and deque by running real Python operations, with results, errors and time complexity.
Python Line-by-Line Executor — Step Execution
Run Python one line at a time: see the line about to run, output as it is printed, changed variables and line counts.