Find a course

Module 3: AlgorithmsLesson 8 of 1213 min

Big O and Algorithm Efficiency

Use Big O notation to describe how running time grows with input size, recognize common complexity classes, and choose algorithms that scale.

Step 1 of 14Learn

Fast today, unusable tomorrow

A startup builds a feature that finds duplicate accounts by comparing every user with every other user. With 1,000 users it takes a blink. A year later they have 1,000,000 users and the job runs for days.

What happened? The number of comparisons is roughly n × n. With 1,000 users, that's 1 million comparisons. With 1,000,000 users, it's 1 trillion: a million times more work for a thousand times more users.

Computer scientists describe this behavior with Big O notation. Big O answers the question: as the input size n grows, how does the work grow? It ignores details that depend on the particular computer or language, such as constant factors, and focuses on the growth rate.

The rules of thumb:

  • Keep the fastest-growing term. An algorithm doing n² + 100n + 5000 steps is O(n²), because for large n the n² term dominates.
  • Drop constant multipliers. 3n steps is O(n). Doubling the speed of a slow algorithm doesn't change its class.
  • Usually think about the worst case. Linear search is O(n) even though sometimes the target is first.

Big O lets you predict, before you write code, whether an approach will still work when the data is 1,000 times larger.

Step 2 of 14Practice · Choose one

An algorithm takes 5n² + 300n + 20 steps for an input of size n. What is its Big O?

Step 3 of 14Learn

The common complexity classes

Most algorithms you'll meet fall into a handful of classes, listed from the slowest-growing amount of work to the fastest-growing:

Big ONameExamplen = 1,000 → about
O(1)ConstantReading an array item by its index1 step
O(log n)LogarithmicBinary search10 steps
O(n)LinearLinear search, summing a list1,000 steps
O(n log n)LinearithmicMerge sort10,000 steps
O(n²)QuadraticComparing every pair; selection sort1,000,000 steps
O(2ⁿ)ExponentialTrying every subset of itemsmore than the atoms in the universe

A few patterns help you recognize them in code:

  • A single loop over the input is usually O(n).
  • A loop inside a loop, both over the input, is usually O(n²).
  • Halving the problem at each step gives O(log n).
  • Doing O(log n) levels of O(n) work each gives O(n log n).

Exponential algorithms are only practical for tiny inputs. Some important problems, like finding the best route that visits many cities, have no known fast algorithm, which is one of the deepest open questions in computer science (the P versus NP problem).

Step 4 of 14Practice · Chart

How work grows with input size

At which input size does n² first need more than 1,000 steps? Tap that point.

Tap the point that answers the question, then press Check.

How work grows with input sizeLine chart with 6 categories: 2: log₂ n 1, n 2, n log₂ n 2, n² 4; 4: log₂ n 2, n 4, n log₂ n 8, n² 16; 8: log₂ n 3, n 8, n log₂ n 24, n² 64; 16: log₂ n 4, n 16, n log₂ n 64, n² 256; 32: log₂ n 5, n 32, n log₂ n 160, n² 1,024; 64: log₂ n 6, n 64, n log₂ n 384, n² 4,096.01,0002,0003,0004,0005,000Steps248163264Input size n
How work grows with input size
Input size nlog₂ nnn log₂ nn²
21224
424816
8382464
1641664256
325321601,024
646643844,096

Step 5 of 14Explore

Growth rate explorer

Compare how the work grows. The curve is c × n² (quadratic); the readouts compare other classes at the same n. Notice how fast n² overtakes n log n.

02,0004,0006,0008,00010,000 020406080100
O(log n) steps
10
O(n) steps
1,000
O(n log n) steps
9,966
c × n² steps
1,000,000

Step 6 of 14Practice · Match the pairs

Match each operation with its typical Big O.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Looking up array item number 500 directly
  2. Binary search in a sorted list
  3. Finding the largest value in an unsorted list
  4. Merge sort
  5. Checking every pair of students for a shared birthday

Step 7 of 14Learn

Reading Big O from code

Let's estimate Big O for three small JavaScript functions.

function first(list) { return list[0]; }

One step, whatever the list size: O(1).

function total(list) {
  let sum = 0;
  for (const x of list) sum += x;
  return sum;
}

One loop over n items: O(n).

function hasDuplicate(list) {
  for (let i = 0; i < list.length; i++)
    for (let j = i + 1; j < list.length; j++)
      if (list[i] === list[j]) return true;
  return false;
}

For each item, compare it with every later item: about n²/2 comparisons, so O(n²).

Can we do better for duplicates? Yes: sort the list first (O(n log n)) and then check neighbors in one pass (O(n)). The total is O(n log n). Using a hash set, a data structure with near-instant lookups, gets it down to about O(n) on average.

This is how efficiency improves in practice: not by writing the same algorithm in a faster language, but by choosing a better algorithm or data structure. A smarter algorithm on a slow laptop can beat a poor one on a supercomputer once the input is big enough.

Step 8 of 14Practice · Work it out

An O(n²) algorithm takes 2 seconds for 10,000 items. Roughly how many seconds will it take for 30,000 items?

Step 9 of 14Chart

An O(n²) job that takes 2 s for 10,000 items

Double the input, four times the time; triple it, nine times. That is quadratic growth.

An O(n²) job that takes 2 s for 10,000 itemsBar chart with 5 categories: 10,000: 2; 20,000: 8; 30,000: 18; 40,000: 32; 50,000: 50.01020304050Seconds10,00020,00030,00040,00050,000Items28183250
An O(n²) job that takes 2 s for 10,000 items
ItemsSeconds
10,0002
20,0008
30,00018
40,00032
50,00050

Step 10 of 14Note

Step 11 of 14Note

Step 12 of 14Practice · Estimate

Use the explorer: at n = 1,000, roughly how many times more steps does n² take than n log₂ n?

75 ×

Step 13 of 14Practice · True or false

If a program uses an O(n²) algorithm, running it on a computer twice as fast changes its Big O to O(n).

Step 14 of 14Practice · What would you do?

What would you do?

Two teams solve the same problem. Team A's algorithm is O(n log n) but has a large constant; it takes 1 second for 1,000 items. Team B's is O(n²); it takes 0.1 seconds for 1,000 items. The product will soon handle 1,000,000 items. Which should they pick?