Search⌘ K
AI Features

A Big-O Drill

Explore the fundamentals of Big-O notation and running-time analysis. This lesson teaches you to prove asymptotic upper bounds of functions and estimate running times of code segments using formal methods and practical examples.

An easy warm up

Claim: nn is O(n)O(n).

To prove this, we want to find positive constants cc' and nn' for which

0ncn for all nn0 \leq n \leq c' n \qquad \text{ for all }n \geq n'

One way to approach a problem

Suppose we want to show that n2+2n+100n^2+2n+100 is O(n2)O(n^2).

Demonstration: To do this, we begin by showing an upper bound on each term of the form c×n2c \times n^2, for some constant cc.

For n1n \geq 1,

n21n22n2n2100100n2\begin{align*} n^2 &\leq 1n^2\\ 2n &\leq 2 n^2\\ 100 &\leq 100 n^2 \end{align*} ...