Courses / Mathematics I
Programming Elements

Computational Complexity

Mathematics I 230 words Free to read

Scaling & Big-O Notation

Two algorithms solve the same problem, but one finishes instantly while the other takes centuries. Computational complexity measures how an algorithm's resource use—usually time (number of basic steps)—grows as input size nn grows.

We describe this growth using big-O notation, which captures the dominant term and drops constants and lower-order terms. For large nn, only the fastest-growing part matters.

ComplexityNameExample
O(1)O(1)constantindexed array access
O(logn)O(\log n)logarithmicbinary search
O(n)O(n)linearsingle loop
O(nlogn)O(n \log n)linearithmicmerge sort
O(n2)O(n^2)quadraticnested loops
O(2n)O(2^n)exponentialdoubling per element
Six classes, the same $n$, recomputed twice as $n$ doubles

The Asymptotic Gulf

The gulf between complexity classes is enormous. For n=1,000,000n = 1{,}000{,}000, a linear algorithm takes a million steps while a quadratic one takes a trillion—seconds versus weeks.

Big-O describes asymptotic behavior (nn \to \infty), so O(n)O(n) and O(100n)O(100n) are the same class. Choosing the right algorithm matters far more than machine speed.

Common pitfall: Confusing an algorithm's complexity class (its scaling trend) with its raw speed on small inputs. Big-O drops constants. A quadratic algorithm might beat a linear one on tiny inputs, but O(n)O(n) is guaranteed to win once nn is large enough.

Practise this lesson

The explanation above is free to read. The graded practice for this lesson lives in the Tryals app.

13practice questions
2interactive scenes

Programming Elements