Counting steps: how algorithms grow
Big-O is a way of saying how the work grows when the input gets bigger, without caring about the machine.
An algorithm is a recipe: a finite list of steps that turns an input into an answer. To compare two recipes you do not time them on one laptop, because a faster machine makes everything faster and tells you nothing. You count steps instead, and you count them as a function of the input size n. That count is what Big-O notation describes.
# Looking for one name in a list of n names. # Linear scan: check one by one. # n = 10 -> up to 10 steps # n = 1000 -> up to 1000 steps # n = 1 000 000 -> up to 1 000 000 steps -> O(n) # Halving: keep the half that can still contain the name. # n = 10 -> up to 4 steps # n = 1000 -> up to 10 steps # n = 1 000 000 -> up to 20 steps -> O(log n) # Doubling the list adds ONE step, not double the work.
O(n) means linear: twice the input, twice the work — checking every name one by one. O(log n) means the work barely grows: doubling the input adds a single step, which is what halving does. O(n²) means a loop inside a loop: ten times the input, a hundred times the work, and that is the one that quietly kills a program at scale.
A long job arrives first. Watch how everything behind it waits.
| Process | Arrival | Burst | State | Used |
|---|---|---|---|---|
| P1 | 0 | 6 | waiting | — |
| P2 | 1 | 4 | waiting | — |
| P3 | 2 | 2 | waiting | — |
| P4 | 3 | 1 | waiting | — |
- P1
P1 arrives with a burst of 6ms
- Avg waiting time
- 0
- Avg turnaround time
- 0
- Total time
- 0
| Algorithm | Avg waiting time | Avg turnaround time | Total time |
|---|---|---|---|
| First-Come, First-Served | 5.5 | 8.75 | 13 |
| Shortest Job First | 4 | 7.25 | 13 |
| Round-Robin | 5.75 | 9 | 13 |
Practice
A list of 1000 names is searched by halving it each time. What is the complexity of this search?
Hints
- Halving 1 000 000 takes about 20 steps, doubling a list adds one more.
- Count steps as a function of n; ignore the machine being fast or slow.
14 XP
Run all the processes in the simulator. Then switch the scheduling algorithm and watch how long the short jobs wait.
Hints
- Switch the algorithm and watch the queue, not just the total time.
- The difference shows up when a long job sits in front of short ones.
16 XP
Match each algorithm with its complexity.
Hints
- n log n sits between n and n squared for large n.
- Two nested loops over the same list multiply the work.
15 XP