SkillGridLearn computer science by doing

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.

14 min · 45 XP

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.

CPU scheduler

A long job arrives first. Watch how everything behind it waits.

Processes
ProcessArrivalBurstStateUsed
P106waiting—
P214waiting—
P322waiting—
P431waiting—
Ready queue
  1. P1

P1 arrives with a burst of 6ms

Avg waiting time
0
Avg turnaround time
0
Total time
0
Compare algorithms
AlgorithmAvg waiting timeAvg turnaround timeTotal time
First-Come, First-Served5.58.7513
Shortest Job First47.2513
Round-Robin5.75913

Practice

  1. 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

  2. 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

  3. 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