Skip to content
Tactiverge

School/○ Innovator/Week 6

Big-O: the shape of growth

Algorithm A takes 3n² + 50n + 1000 steps; algorithm B takes 400n. Which would you choose for a map with a million cells?

About 40 min · 3 goals · Checked by automated tests

1.Why should I care?

Big-O is the engineer's shorthand for 'how does the cost grow?'. It ignores details that don't matter for large inputs, so you can compare designs on paper and spot the one that will collapse when the map, the log or the parts list gets ten times bigger.

  • ◇ Express an operation count in big-O notation by keeping the fastest-growing term and dropping constants.
  • ◇ Rank O(1), O(log n), O(n), O(n log n), O(n²) and O(n³) and predict the effect of doubling n.
  • ◇ Use a measured time and a growth rate to predict the time for a larger input.

2.Watch

→ Watch why constants and smaller terms are dropped, and how nested loops lead to O(n²).

Explains why big-O drops constants and smaller terms, with worked examples of loops and nested loops.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

3.Predict

A = 3n² + 50n + 1000 steps, B = 400n steps. For n = 1,000,000, roughly how many times more steps does A take?

Guess first. Being wrong here is how the surprise works.

4.Discover

Switch on the logarithmic y-axis so all four curves fit. Then raise the constant in front of n log n to 20 and find where n² overtakes it. What does that tell you about small inputs versus large ones?

Loading the interactive…

Can't use the interactive? Read a description instead

The plotter shows log n, n, n log n and n² against n, optionally on a logarithmic y-axis. A slider multiplies n log n by a constant; with a constant of 20, n² is cheaper until about n = 145, after which n² grows far faster.

5.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

An exact count is T(n) = 7n + 3n·log₂n + 12. What is its big-O?

Practice 2 · a fresh version every time

Timing tests show a collision checker grows like n and takes 5 ms for n = 4096. Assuming that growth holds, predict its time for n = 8192, in ms.

ms

6.Understand

Keep the top term, drop the constant

From an exact count, keep the term that grows fastest and drop its coefficient: 5n + 20 → O(n); n(n − 1)/2 → O(n²); 4 log₂ n + 7 → O(log n). Big-O describes the shape of growth, not the exact number of steps.

The usual ladder

O(1) constant (dict lookup) < O(log n) (binary search) < O(n) (one scan) < O(n log n) (good sorts) < O(n²) (all pairs, insertion sort) < O(n³) (three nested loops). Doubling n adds a step, doubles, slightly more than doubles, quadruples, and multiplies by 8.

Predicting from a measurement

If you measure 20 ms at n = 1,000 and the code is O(n²), expect about 20 × 4 = 80 ms at n = 2,000 and 20 × 100 = 2 s at n = 10,000. Then test the prediction: if the measurement disagrees, your model of the code is wrong somewhere.

Explain it another way

Big-O is like describing a hill by its steepness rather than its exact height: a steep hill always ends up higher eventually, however far back it starts.

Derivation: Why constants and smaller terms are dropped
  1. Take T(n) = 3n² + 50n + 1000. Divide by n²: T(n)/n² = 3 + 50/n + 1000/n².

  2. As n grows, 50/n and 1000/n² shrink towards 0, so T(n)/n² approaches 3. The n² term ends up doing almost all the work.

  3. Big-O asks only how T grows, up to a constant factor: T(n) ≤ c·n² for some constant c once n is large enough (here c = 4 works for n ≥ 66). So T(n) is O(n²).

  4. The constant 3 is dropped because it depends on the machine and the language; the exponent 2 doesn't. Doubling n multiplies any O(n²) cost by about 4, whatever the constant.

Worked example 1

Predict from a measurement for three growth rates.

import math


def predict(t_ms, n_old, n_new, growth):
    f = {
        "n": lambda n: n,
        "n log n": lambda n: n * math.log2(n),
        "n^2": lambda n: n * n,
    }[growth]
    return t_ms * f(n_new) / f(n_old)


for g in ["n", "n log n", "n^2"]:
    print(g, round(predict(20, 1024, 4096, g), 1))
# n 80.0
# n log n 96.0
# n^2 320.0

Common mix-ups

  • O(n) means exactly n steps.
    ✓ It means the steps grow in proportion to n: 3n + 100 is O(n).
  • Lower big-O always means faster code.
    ✓ Only for large enough inputs. For small inputs, constants and overheads can dominate.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3 · a fresh version every time

Timing tests show a parts-matching script grows like n² and takes 12 ms for n = 1024. Assuming that growth holds, predict its time for n = 4096, in ms.

ms

Practice 4

Which list is in order from slowest-growing to fastest-growing?

Practice 5

Algorithm X is O(n log n) and Y is O(n²). Which statement is true?

8.Apply it

Write the big-O of each function in your planner package (loading, neighbours, BFS, report). Pick the one with the worst growth and predict how its time changes when the map grows from 50 × 50 to 200 × 200. Keep the prediction for the next lesson's measurements.

9.Reflect

Concepts: Algorithms, searching and complexity