- n = 3
- steps = 0
- for i in range(n):
- for j in range(n):
- steps += 1
- print(steps, "steps")
Variables
- n
- 3 (just changed)
n is the class size. Change it and watch the count.
nThis 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.
n- n = 4
- steps = 0
- for i in range(n):
- steps += 1
- print(steps, "steps")
Variables
- n
- 4 (just changed)
Set n to 4.
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.
| n | 3n + 5 | n² + n + 100 | n² alone |
|---|---|---|---|
| 10 | 35 | 210 | 100 |
| 100 | 305 | 10,200 | 10,000 |
| 1,000 | 3,005 | 1,001,100 | 1,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.
n- n = 16
- steps = 0
- while n > 1:
- n = n // 2
- steps += 1
- print(steps, "halvings")
Variables
- n
- 16 (just changed)
Set n to 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.
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.
n- calls = [0]
- def fib(k):
- calls[0] += 1
- if k <= 1: return k
- a = fib(k-1)
- return a + fib(k-2)
- n = 3
- print(fib(n), calls[0])
Variables
calls
A one-item list, so fib can add to it from inside.
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):
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).
Most of the time, the shape of the code tells you its Big O:
| Code shape | Big O | Example |
|---|---|---|
| A fixed number of steps, no loop over n | O(1) | marks[3] |
| n halves on every pass | O(log n) | Binary search |
| One loop over n | O(n) | Find the maximum |
| A loop over n with a halving loop inside | O(n log n) | Merge sort (later) |
| A loop over n inside a loop over n | O(n²) | Compare every pair |
| Two recursive calls, n shrinks by 1 | O(2^n) | Plain Fibonacci |
Merge sort halves the list about log n times and does n steps of merging at each level.
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 O | n = 10 | n = 100 | n = 1,000 |
|---|---|---|---|
| O(1) | 1 | 1 | 1 |
| O(log n) | 3 | 7 | 10 |
| O(n) | 10 | 100 | 1,000 |
| O(n log n) | 33 | 664 | 9,966 |
| O(n²) | 100 | 10,000 | 1,000,000 |
| O(2^n) | 1,024 | about 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?
target- marks = [72, 85, 64, 91, 58]
- target = 64
- steps = 0
- found = -1
- for i in range(len(marks)):
- steps += 1
- if marks[i] == target:
- found = i
- break
- print(found, "after", steps)
Variables
marks
Set marks to [72, 85, 64, 91, 58].
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.