The whole section compares algorithms with big O notation: O(1), O(N), O(log N), O(N²). Behind those symbols hides very little math — powers, logarithms and the idea of "how fast a function grows". Most of it was taught at school and then never used again for years. This article is a short refresher from zero, so that log N and N² read without stumbling later on.
Powers: N squared and two to the power of N
A power is shorthand for multiplying a number by itself. N² ("N squared") is N × N, N³ is N × N × N, and N to the power of k is N multiplied by itself k times.
The difference between N² and 2ⁿ is fundamental, even though at N = 10 the numbers look close (100 against 1024). The easiest way to see it is to compute both as N grows:
live example
public class Growth {
public static void main(String[] args) {
for (int n = 10; n <= 30; n += 10) {
System.out.println("N=" + n + " N squared = " + (long) n * n + " 2^N = " + (1L << n));
}
}
}
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
At N = 30 the square is 900 — still nothing. Two to the power of 30 is already a billion.
- In
N²the base grows while the exponent is fixed (two). Double the data and the work grows fourfold. That is quadratic growth: fast, but predictable. - In
2ⁿthe base is fixed (two) and the exponent grows — N itself. Every extra element doubles the result. That is exponential growth, and it explodes: at N = 30 it is already over a billion, at N = 60 it is more than the number of seconds since the Big Bang.
N² is the area of a square with side N: an N×N grid. Double the side and you get four times the cells. Hence the name "quadratic".
Logarithms: the other side of a power
A logarithm answers the reverse question. A power asks: "what is 2 to the power of 3?" (answer: 8). A logarithm asks the opposite: "to what power must 2 be raised to get 8?" (answer: 3). It is written log₂ 8 = 3.
For algorithms a different definition of the same thing is handier:
log₂ Nis how many times N has to be halved to reach 1.
16 → 8 → 4 → 2 → 1: four halving steps, so log₂ 16 = 4. This is exactly how binary search works — every step throws away half of the data.
Two consequences follow:
- A logarithm grows very slowly. Data grew a thousandfold — the number of halvings went up by only 10 (
log₂ 1000 ≈ 10, because2¹⁰ = 1024). For a millionlog₂ 1 000 000 ≈ 20, for a billion it is 30. That is why an algorithm running inlog Nbarely notices the data growing. - The base of the logarithm does not matter for big O.
log₂ Nandlog₁₀ Ndiffer only by a constant factor, and constants are dropped in big O. That is why people simply writeO(log N)without naming the base.
You can check that by plain counting: halve the number until it reaches 1 and count the steps.
live example
public class Halvings {
public static void main(String[] args) {
for (long n : new long[]{16, 1024, 1_048_576}) {
long value = n;
int steps = 0;
while (value > 1) {
value /= 2;
steps++;
}
System.out.println(n + ": halvings = " + steps);
}
}
}
Run
Running examples is part of paid access. There the same code runs inside the article: editor, run and check next to the paragraph. Three free days →
A million and change takes twenty steps. Estimating log₂ N in your head is easy too: it is the number of binary digits minus one, that is, the logarithm rounded down. Three landmarks are worth memorising: log₂ 1000 ≈ 10, log₂ 1 000 000 ≈ 20, log₂ 1 000 000 000 ≈ 30.
How fast different functions grow
The whole point of big O is how a function behaves when N gets large. Here are the main growth rates from the slowest to the fastest, with their values at N = 10, 100 and 1000:
| Function | Name | N=10 | N=100 | N=1000 |
|---|---|---|---|---|
1 | constant | 1 | 1 | 1 |
log N | logarithmic | ≈3 | ≈7 | ≈10 |
N | linear | 10 | 100 | 1000 |
N·log N | linearithmic | ≈33 | ≈664 | ≈9966 |
N² | quadratic | 100 | 10 000 | 1 000 000 |
2ⁿ | exponential | 1024 | ≈10³⁰ | more than the atoms in the universe |
The gap between the rows is the difference between "instant" and "never finishes". On a thousand elements a logarithm is 10 steps and a square is a million; on a million the gap is already a hundred thousand times.
The lower the curve, the better the algorithm on large N. O(1) and O(log N) hug the floor; O(N²) and O(2ⁿ) shoot into the ceiling — such algorithms die on modest volumes already.
How big O is read off a formula
Big O takes the formula for the number of operations and keeps only the part that decides how it grows on large N. There are two rules:
- Drop constants and factors.
3Nand100Nare bothO(N): twice the data means twice the work, the coefficient does not matter. - Keep the fastest growing term. In the sum
N² + N + 100everything is decided byN²when N is large: at N = 1000 that is1 000 000 + 1000 + 100, and the smaller terms vanish. So it isO(N²).
| Actual number of steps | What it is called |
|---|---|
| 5N + 3 | O(N) |
| N² + 10N + 7 | O(N²) — N² outweighs everything else |
| 2·N·log N | O(N·log N) |
| constant 42 | O(1) |
Why so crude? Because big O answers not "how many milliseconds" but "what happens once there is a lot of data". On small N every algorithm is fast; the difference shows up on large ones.
In short
- A power
N²isN×N(quadratic growth: double the data, four times the work).2ⁿis two multiplied by itself N times (exponential, explosive growth). - A logarithm
log₂ Nis how many times N is halved to reach 1. It grows very slowly: a million is ~20, a billion is ~30. The base does not matter for big O. - Growth rates in order:
1 < log N < N < N·log N < N² < 2ⁿ. On large N the gap between them is enormous. - Big O is read like this: drop constants and factors, keep the fastest growing term.
3N² + 5N + 9 → O(N²). N·log Nis almost linear. On a million elements it costs only 20 times more thanNand 50 000 times less thanN².- The measure is about large N. On a hundred elements everything is fast; the gap between
O(N)andO(N²)appears as the data grows — which is why constants are dropped.
What to read next
- Arrays, binary search and big O — where this math first pays off: why binary search is
O(log N). - Recursion — divide and conquer, the source of
N·log Nin the fast sorting algorithms. - Advanced sorting — how algorithms move from
O(N²)toO(N·log N). - Choosing a data structure — where these estimates turn into a decision.