Chapter 4: Complexity, Memory and Performance Analysis
4.01Beginner9 topics20 min5 interactive animations

Asymptotic notation

Asymptotic notation describes how cost grows for large inputs while ignoring constants. This lesson defines O, Ω, Θ, o and ω precisely, explains upper, lower and tight bounds, dominant terms and hidden constants, and lets you test the definitions with explicit witnesses.

Learning goals

  • State the formal definitions of O, Ω and Θ
  • Find witnesses c and n₀ for a bound
  • Explain why constants and lower-order terms are dropped, and when they matter anyway

Prerequisites

Arithmetic essentialsSummations and sequences
4.01.1

Big O

f(n) = O(g(n)) means there are constants c > 0 and n₀ such that f(n) ≤ c · g(n) for all n ≥ n₀. It is an upper bound: f grows no faster than g, up to a constant factor. Example: 3n² + 5n + 7 = O(n²) with c = 4 and n₀ = 7 (check: 3n² + 5n + 7 ≤ 4n² when n² ≥ 5n + 7, true for n ≥ 7).

Use the explorer: choose f and g, then move c and n₀ until the green region shows f below c · g from n₀ onward. That pair is a witness.

Big-O and Big-Ω witness explorer

Interactive · drag the sliders
n₀ = 10110203040
f(n) = 3n² + 10n + 50c · g(n) = 4.0 · n²violations

Not a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.

4.01.2

Big Omega

f(n) = Ω(g(n)) means f(n) ≥ c · g(n) for all n ≥ n₀: a lower bound. Example: 3n² + 5n + 7 = Ω(n²) with c = 3 and n₀ = 1, since the extra terms are positive.

Ω is used for “at least this much work”: any comparison sort is Ω(n log n) in the worst case. Switch the explorer to “Lower bound” to look for a c and n₀ with f above c · g.

Big-O and Big-Ω witness explorer

Interactive · drag the sliders
n₀ = 10110203040
f(n) = 3n² + 10n + 50c · g(n) = 4.0 · n²violations

Not a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.

4.01.3

Big Theta

f(n) = Θ(g(n)) means f is both O(g) and Ω(g): it is bounded above and below by constant multiples of g for large n. So 3n² + 5n + 7 = Θ(n²), with constants 3 and 4. Θ states exactly how a cost grows.

Saying “binary search is O(n)” is true but weak; “Θ(log n) in the worst case” is precise.

NotationMeaningInformal
f = O(g)f ≤ c·g eventuallygrows at most as fast
f = Ω(g)f ≥ c·g eventuallygrows at least as fast
f = Θ(g)bothgrows at the same rate
f = o(g)f / g → 0grows strictly slower
f = ω(g)f / g → ∞grows strictly faster

Big-O and Big-Ω witness explorer

Interactive · drag the sliders
n₀ = 10110203040
f(n) = 3n² + 10n + 50c · g(n) = 4.0 · n²violations

Not a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.

4.01.4

little-o

f(n) = o(g(n)) means f grows strictly slower than g: for every c > 0 there is an n₀ with f(n) < c · g(n) for n ≥ n₀, equivalently f(n)/g(n) → 0. So n = o(n²) and log n = o(n), but n² ≠ o(n²).

Little-o is a strict inequality between growth classes, while big-O allows equal growth.

4.01.5

little-omega

f(n) = ω(g(n)) means f grows strictly faster than g: f(n)/g(n) → ∞. So n² = ω(n) and 2ⁿ = ω(n¹⁰⁰). It is the strict counterpart of Ω.

f = ω(g) holds exactly when g = o(f).

4.01.6

Upper and lower bounds

An upper bound on an algorithm says it never costs more than this; a lower bound says it never costs less. A problem has its own bounds too: a problem's upper bound is the cost of the best known algorithm, and its lower bound is what any algorithm must spend. When they meet, the problem is solved optimally; comparison sorting has both at Θ(n log n).

Bounds should say which case they cover: worst, average or best.

StatementKind
Merge sort runs in O(n log n) in every casealgorithm upper bound
Any comparison sort needs Ω(n log n) comparisonsproblem lower bound
Therefore merge sort is optimal among comparison sortsbounds match
4.01.7

Tight bounds

A bound is tight when it is Θ: it cannot be improved by more than a constant factor. A loose bound is still correct but unhelpful, such as saying insertion sort is O(n³). Always report the tightest bound you can prove.

Test the tightness of a witness in the explorer: if you can also find a lower witness with the same g, the bound is tight.

Big-O and Big-Ω witness explorer

Interactive · drag the sliders
n₀ = 10110203040
f(n) = 3n² + 10n + 50c · g(n) = 4.0 · n²violations

Not a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.

4.01.8

Dominant terms

In a sum of terms, the fastest-growing term dominates for large n, so lower-order terms are dropped: 5n³ + 100n² + 1,000n + 10⁶ = Θ(n³). The order from slowest to fastest is 1, log n, √n, n, n log n, n², n³, 2ⁿ, n!.

Be careful: the cost of different phases adds, so it is O(max), but costs inside nested loops multiply.

CostSimplified
n² + n log n + 50Θ(n²)
3 · 2ⁿ + n⁵Θ(2ⁿ)
n log n + nΘ(n log n)
n phases of O(n) workΘ(n²)
4.01.9

Hidden constants

Big-O hides constants, and constants decide real speed for realistic n. An algorithm that costs 1000n beats 2n² only when n > 500. Cache misses, allocation and memory layout contribute large constants, so a “slower” O(n log n) on arrays can beat an O(n) algorithm on a linked structure for medium n.

Use the crossing-point view of the explorer: move c and see where two functions swap order.

Java
1// 1000*n vs 2*n*n: equal when n = 500
2long lin = 1000L * n, quad = 2L * n * n;
3boolean linearWins = lin < quad; // true for n > 500

Big-O and Big-Ω witness explorer

Interactive · drag the sliders
n₀ = 10110203040
f(n) = 3n² + 10n + 50c · g(n) = 4.0 · n²violations

Not a witness. f(n) exceeds c·g(n) at n = 10 and 3 more points. With this c the inequality holds from n₀ = 14: move the slider there.

Practice ladder

Now try it yourself

Easy

  • Find c and n₀ showing 5n + 20 = O(n)
  • Simplify 4n³ + n² log n + 99

Medium

  • Prove that n log n = O(n²) but n² ≠ O(n log n)
  • Is 2ⁿ⁺¹ = O(2ⁿ)? Is 2²ⁿ = O(2ⁿ)?

Advanced

  • Show that log(n!) = Θ(n log n) using the bounds (n/2)^(n/2) ≤ n! ≤ nⁿ

Key takeaways

  • O is an upper bound, Ω a lower bound, Θ both; small o and ω are strict.
  • Keep the dominant term and drop constants, but remember constants decide real speed.
  • Report the tightest bound and say which case it covers.