← Course home

Foundations · Lesson 2 of 52

Asymptotic growth

Use upper, tight and lower bounds to compare how algorithm work grows.

Full lessonAbout 10 min

You will learn to distinguish O, Θ and Ω and compare common growth rates.

Recommended first: Input size and operation counts.

On this page 6 sections

The idea

At n = 64, n log2 n is 384 and n squared is 4096.

An exact count such as 3n + 20 is useful, and it is also more precise than the decision you usually need. Big O, Θ, and Ω compare growth. O(f) says the work is at most a constant times f for large n. Ω(f) says it is at least a constant times f. Θ(f) says both, so f is a tight description. 3n + 20 is Θ(n) because (3n + 20) / n approaches 3. It is also O(n²), since a looser upper bound is still true, but Θ(n²) is false. When you write a cost in this guide, prefer the tight bound and say so.

A worked trace

StepStateWhat changed
n=4n = 4At n = 4, n log2 n is 8 and n squared is 16.
n=16n = 16At n = 16, n log2 n is 64 and n squared is 256.
n=64n = 64At n = 64, n log2 n is 384 and n squared is 4096.
n=256n = 256At n = 256, n log2 n is 2048 and n squared is 65536.

Why it is correct

The limit test is the reason the constant and the added 20 disappear. For any fixed c, (cn + d) / n approaches c. A function is Θ(n) when two positive constants sandwich it between multiples of n for all large n. 3n + 20 sits between 3n and 4n once n is at least 20. It does not sit between two multiples of n², because (3n + 20) / n² approaches 0.

A common mistake

A looser upper bound is still true, so O(n squared) does not mean the tight bound is n squared.

What it costs

Using the notation does not change the algorithm. It changes the claim. A Θ(n) scan of a million items is a different engineering choice from a Θ(n²) pair scan of the same list. The trace shows n squared pulling away from n log n as n leaves the toy range. Comparing the formulas uses O(1) extra memory.

Write the exact count first, as the previous lesson did for pairs. Only then replace it with Θ. If you start from Θ you can hide a bug such as an extra nested loop. The notation is a compression step, not a substitute for the count.

Run it

python
from bitsandbytes.complexity import growth_tableprint(growth_table((64,))[0])# prints: (64, 384, 4096) 

Practice and next step

Open Cost models and assumptions next. Cost models ask which Python operation that bound is actually counting.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.complexity

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Compare growth

The same n is plugged into three formulas. Read which one pulls away.

n
4

State

  1. 04chosen
  2. 18chosen
  3. 216chosen

At n = 4, n log2 n is 8 and n squared is 16.

Practice with feedback

Check your understanding

Try each checkpoint before revealing the feedback. Ask for a hint when you need one. Answers and hints stay in this browser when local storage is available.

1For large n, which grows faster?
2At n = 16, n log2 n is 64 and n squared is 256. Which comparison holds?

Progress is never sent to an account or synced to another device. If local storage is blocked, choices last only until you leave this page.