What does Big O notation actually measure?

Big O notation measures the growth rate of an algorithm's resource usage (usually time or memory) relative to the size of its input. Instead of telling you exactly how many seconds a program will take to run, it tells you how much longer it will take if you double, triple, or massively increase the amount of data it has to process.

Think of it like buying groceries. If you buy a single item, it takes a certain amount of time. If you buy ten times as many items, does your shopping time stay the same, increase by ten times, or take a hundred times longer because you have to mathematically compare every item to every other item in your cart? Big O gives us a standardized mathematical way to describe this scaling behavior.

The Search for the Dominant Term

When we analyze an algorithm, we look for the operation that takes the most time as the input, which we call nn, gets infinitely large. This is called the dominant term. If an algorithm takes n2+5n+10n^2 + 5n + 10 steps, the n2n^2 part will completely overshadow the 5n5n and the 1010 when nn is a million. Big O isolates this dominant term, letting us say the algorithm is O(n2)O(n^2).

Why We Drop the Constants

In Big O notation, we ignore constants and coefficients. An algorithm that takes 50n50n steps and one that takes 2n2n steps are both considered O(n)O(n), or linear time. This can feel weird at first. Why treat them the same? Because Big O only cares about the shape of the growth curve. Hardware speeds and language efficiencies change the constant multipliers, but an O(n)O(n) algorithm will eventually always beat an O(n2)O(n^2) algorithm for large enough inputs, regardless of the constants.

Where Students Slip Up

The most common mistake is thinking Big O measures exact time in seconds or exact operations. A student might test their O(n2)O(n^2) code on 10 items, see it finishes in 0.001 seconds, and think it is perfectly efficient. But Big O is about scaling. If that same code runs on 100,000 items, the time doesn't scale linearly; it scales quadratically, and those 0.001 seconds could suddenly become hours.

Worked through

Determine the Big O time complexity of a function that takes an array of size nn, prints every item once, and then compares every item to every other item to check for duplicates.

First, we break down the operations. Printing every item in the array takes nn steps. Comparing every item to every other item requires a nested loop: for each of the nn items, we look at the other nn items. This takes nimesn=n2n imes n = n^2 steps.

The total number of steps is n2+nn^2 + n.

Next, we apply the rules of Big O notation. We look at what happens as nn gets very large. The n2n^2 term grows much faster than the nn term, making it the dominant term. We drop the smaller nn term.

Therefore, the time complexity is O(n2)O(n^2).

Questions students ask

Ask about this topic

Where this comes from: Introduction to Algorithms (Cormen, Leiserson, Rivest, Stein) · Khan Academy: Algorithms Unit · Grokking Algorithms (Aditya Bhargava)

See also