Visualize and compare time complexity curves, with code examples and a speed-improvement calculator
Quadratic — nested loops; doubles input means 4× the work.
// O(n²) — bubble sort
function bubbleSort(arr: number[]): number[] {
const a = [...arr];
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
[a[j], a[j + 1]] = [a[j + 1], a[j]];
}
}
}
return a;
}
// Two nested loops: every pair of elements
// is compared. n=1000 → ~500,000 comparisons.If computers became faster, how many more values could the same algorithm handle in the same time? Based on n = 10.
| Complexity | Ops at n=10 | 1,000× faster → n | 1,000,000× faster → n |
|---|---|---|---|
| O(1) | 1 | ∞ | ∞ |
| O(log n) | 3 | 10k | 10.0M |
| O(n) | 10 | 10k | 10.0M |
| O(n log n) | 33 | 3k | 1.6M |
| O(n²) | 100 | 316 | 10k |
| O(n³) | 1.0k | 100 | 1k |
| O(2ⁿ) | 1.0k | 20 | 30 |
| O(n!) | 3.6M | 12 | 15 |