Every student vs every studentline 1
  1. n = 3
  2. steps = 0
  3. for i in range(n):
  4. for j in range(n):
  5. steps += 1
  6. print(steps, "steps")

Variables

n
3 (just changed)

n is the class size. Change it and watch the count.

Step 1 of 28
Try another n

This code compares each student's marks with every student's marks, including their own. Drag n from 1 to 3: the steps go 1, 4, 9, which is n times n.

Quick check

Quick check: The same code runs for a class of 10 students. How many steps?

One loop against two#

Big O describes how the number of steps grows as the input size n grows. Unless someone says otherwise, people quote it for the worst case: the input that makes the code take the most steps. It ignores seconds, because seconds depend on the laptop and the language, and steps do not. Here is one loop over the same students. Drag n and compare the count with the opener.

One loop: one step per studentline 1
Try another n
  1. n = 4
  2. steps = 0
  3. for i in range(n):
  4. steps += 1
  5. print(steps, "steps")

Variables

n
4 (just changed)

Set n to 4.

Step 1 of 12

One loop takes n steps, so it is O(n).

Quick check

Quick check: Your class grows from 50 to 100 students. The nested loop took 2,500 steps. How many now?

Drop the constants and the small terms#

Say one program takes 3n + 5 steps and another takes n² + n + 100. Watch which term takes over as n grows.

n3n + 5n² + n + 100n² alone
1035210100
10030510,20010,000
1,0003,0051,001,1001,000,000

At n = 1,000, the n² term is almost all of the total. So we keep only the fastest growing term and drop its constant: 3n + 5 is O(n), and n² + n + 100 is O(n²). Big O asks how the count grows. The 3 and the + 5 change the count, not how it grows.

Quick check

Quick check: A program loops over n marks to find the total, then loops over them again, separately, to find the maximum. What is its Big O?

Halving: O(log n)#

Some code throws away half of what is left on every pass. Drag n from 1 to 64 and count the halvings.

How many times can n be halved?line 1
Try another n
  1. n = 16
  2. steps = 0
  3. while n > 1:
  4. n = n // 2
  5. steps += 1
  6. print(steps, "halvings")

Variables

n
16 (just changed)

Set n to 16.

Step 1 of 16

16 takes 4 halvings, 32 takes 5 and 64 takes 6. Doubling n adds just one step. log₂ n is how many times you can halve n before you reach 1. log₂ 64 is 6, so this is O(log n). Binary search works this way: every look at the middle drops half of the list.

Visualizer

Binary Search

Open full →

Quick check

Quick check: A sorted list holds 10 lakh roll numbers. About how many halvings bring it down to one?

Two calls each time: O(2^n)#

A recursive function calls itself on a smaller input until it reaches a base case that answers directly. When it calls itself twice and n shrinks by only 1, the calls pile up fast. Here is plain recursive Fibonacci: a holds the first answer while the second call runs. Drag n from 1 to 4 and watch the call count.

Plain recursive Fibonacciline 1
Try another n
  1. calls = [0]
  2. def fib(k):
  3. calls[0] += 1
  4. if k <= 1: return k
  5. a = fib(k-1)
  6. return a + fib(k-2)
  7. n = 3
  8. print(fib(n), calls[0])

Variables

calls

0

A one-item list, so fib can add to it from inside.

Step 1 of 23

The calls go 1, 3, 5, 9, then 15 for n = 5, growing about 1.6 times per step of n. fib(30) makes about 27 lakh calls. Big O is an upper bound: for large n, the count grows no faster than 2^n, give or take a constant factor. The true rate is about 1.6^n, so 2^n is a safe ceiling, and plain Fibonacci is O(2^n). A memo fixes this. It stores each answer the first time it is computed, so each value is worked out once, and the time drops to O(n):

Visualizer

Recursion vs Memoization

Open full →

Read the shape of the code#

Some steps do not depend on n at all. marks[3] takes one step whether the list has 5 marks or 5 lakh, because the computer works out where index 3 is straight from the index. That is O(1).

728564915801234
marks

Most of the time, the shape of the code tells you its Big O:

Code shapeBig OExample
A fixed number of steps, no loop over nO(1)marks[3]
n halves on every passO(log n)Binary search
One loop over nO(n)Find the maximum
A loop over n with a halving loop insideO(n log n)Merge sort (later)
A loop over n inside a loop over nO(n²)Compare every pair
Two recursive calls, n shrinks by 1O(2^n)Plain Fibonacci

Merge sort halves the list about log n times and does n steps of merging at each level.

Visualizer

Merge Sort

Open full →

Quick check

Quick check: A loop runs i from 1 to n, and inside it a while loop halves a copy of n down to 1. What is the Big O?

The growth table#

Here are the step counts at three sizes. log is base 2, and every count is rounded to a whole number.

Big On = 10n = 100n = 1,000
O(1)111
O(log n)3710
O(n)101001,000
O(n log n)336649,966
O(n²)10010,0001,000,000
O(2^n)1,024about 10³⁰about 10³⁰¹

Quick check

Quick check: In a placement coding test, n is 1 lakh (10⁵). Roughly 10⁸ simple steps fit in one second. Which solution finishes in time?

Best, average and worst case#

Linear search checks marks one by one until it finds the target. How many checks it needs depends on where the target sits.

Quick check

Quick check: marks = [72, 85, 64, 91, 58]. The target is 100, which is not in the list. How many checks?

Linear search, counting checksline 1
Try another target
  1. marks = [72, 85, 64, 91, 58]
  2. target = 64
  3. steps = 0
  4. found = -1
  5. for i in range(len(marks)):
  6. steps += 1
  7. if marks[i] == target:
  8. found = i
  9. break
  10. print(found, "after", steps)

Variables

marks

7285649158

Set marks to [72, 85, 64, 91, 58].

Step 1 of 16

Try 72, the first position: 1 check. That is the best case, O(1). Try 58, the last position, or 100: 5 checks, n in general. That is the worst case, O(n). On average, if the target is equally likely to be anywhere, it takes about n/2 checks. That is the average case, still O(n).

One last check#

Quick check

Quick check: For n hostel rooms, your code checks every pair of rooms for a booking clash, then prints each room once. What is its Big O?

Next is space complexity, which uses the same Big O idea to measure how much extra memory your code needs as n grows.