Data Structures & Algorithms / Module 0: Thinking in cost

Topic 0.2

Big-O

A way to estimate how much more work code does when the data grows.

10 min · beginner

Watch: slide the amount of data to the right and compare the five pillars. Some barely move; some shoot upwards.

11 K1 M1 B1 T1 secondO(1)O(log n)O(n)O(n log n)O(n²)
Pillar height: number of steps (log scale, ×10 per line)1-second line: 100 million stepsFive pillars do the work for the same n items. The board is logarithmic: one line up means 10 times more steps. Times assume a machine that does about 100 million simple steps per second.
RateStepsEstimated time
O(1) constant1< 1 µs
O(log n) logarithmic10< 1 µs
O(n) linear1,00010.0 µs
O(n log n) n log n10,000100 µs
O(n²) quadratic1,000,00010.0 ms

Picture looking for a friend in a classroom

There are four ways to look, and each grows differently as the class gets bigger:

  • Your friend always sits in the front seat. You walk straight there. Ten people or a thousand, the work is the same. That is O(1).
  • The register is sorted by name. You open it in the middle, see whether the name comes before or after, and throw away half. A thousand people take about 10 looks. That is O(log n).
  • You ask people one at a time. A class twice as big means twice the work. That is O(n).
  • You want to know whether two people share a birthday, and you do it by comparing everyone with everyone else. A class twice as big means four times the work. That is O(n²).

The letter n just means "how many items". Big-O ignores how fast the computer is; it only looks at how the work grows as n grows.

Why it matters

Move n in the figure. With 100 items all five pillars are far below the red 1-second line, so everything feels fast. With 1,000,000 items, O(n²) needs about 2.8 h, while O(n log n) is done in 200 ms. Both programs give the right answer; the only difference is how their work grows. That is why a program that runs smoothly on a laptop with sample data can grind to a halt on a real server.

Rate10× more data, the work…
O(1)stays the same
O(log n)grows by a few steps
O(n)is 10× more
O(n log n)is a bit more than 10×
O(n²)is 100× more

What it looks like in code

You don't need formulas. Usually the shape of the loops tells you:

Code, for programmersNo loop: O(1)
function first(list) {
  return list[0];          // one step, however long the list is
}
Code, for programmersOne loop over all the data: O(n)
function total(list) {
  let sum = 0;
  for (const x of list) sum += x;   // one step per item
  return sum;
}
Code, for programmersA loop inside a loop over the same data: O(n²)
function hasDuplicate(list) {
  for (let i = 0; i < list.length; i++)
    for (let j = i + 1; j < list.length; j++)   // every pair is compared
      if (list[i] === list[j]) return true;
  return false;
}
Code, for programmersSorting with the built-in sort: O(n log n)
const sorted = [...list].sort((a, b) => a - b);

Quick rules of thumb

  • Nested loops multiply. A loop inside a loop over the same data means n × n.
  • Loops one after another add. Two separate loops are still O(n), because n + n is only double.
  • Multipliers are ignored. 3n and n are both O(n). The number still matters in real life, but it doesn't change whether the code blows up as the data grows.
  • The biggest part wins. If part of the code is O(n²) and part is O(n), the whole thing is O(n²).

Quiz

  1. A function has two separate (not nested) loops, each over n items. What is its Big-O?

  2. Which is slowest for 1,000,000 items?

  3. Roughly how many "looks" does it take to find one name in a sorted list of 1,000,000 names, if every look throws away half the list?

  4. Code calls list.includes(x) inside a for loop over the same list. What is its Big-O?

0 of 4 answered.