Week 01 · Phase 1 · Thinking in steps

What Is an Algorithm?

Recipes, step-by-step thinking, and why two correct methods can be wildly different.

Big question: If two people both get the right answer, why would we prefer one method?no programming neededColab setup≈2 hours
By the end of this week you can
  • say what an algorithm is, and give three examples from your own day;
  • write a set of instructions precise enough that a stranger could follow them;
  • count the steps of a method by hand, for a small input and for a big one;
  • explain why two correct methods can be worlds apart in cost;
  • spot the three habits that make a described method ambiguous, endless or wrong;
  • open Google Colab and run your first line of Python.

1.1An algorithm is a recipe

An algorithm is a finite list of clear instructions that turns some input into some output. That is the whole definition. Making tea is an algorithm. Long division is an algorithm. The instructions for assembling a bookshelf are an algorithm — a bad one, but an algorithm.

Four things make a list of instructions count:

A second picture, if the kitchen one does not land: think of giving someone directions. "Head towards the tall building" depends on which building they are looking at; "turn left after 300 metres onto Mill Road" does not. Good directions and good algorithms share the same virtue — the person following them never has to guess what you meant. A computer is the most literal follower you will ever meet: it does exactly what you wrote, never what you intended.

Try this before reading on

Write instructions for making a cheese sandwich, precise enough for someone who has never seen bread. Most people write six steps. Most people's six steps produce a person holding an unopened packet, because "put cheese on bread" assumes the packet is open. That gap — between what you meant and what you said — is the entire difficulty of programming, and you have now met it in week 1.

1.2The dictionary, and the whole course in one example

Suppose you have a paper dictionary of 1000 pages and you want the word "quokka". Here are two correct methods.

Method A — page by page

  1. Open page 1. Is "quokka" here? No.
  2. Open page 2. Is it here? No.
  3. … keep going until you find it.

This works. It always works. For "quokka", sitting somewhere around page 700, it costs about 700 page-opens.

Method B — open in the middle

  1. Open the middle page. Are we past "quokka" alphabetically? If yes, throw away the right half; if no, throw away the left half.
  2. Open the middle of what is left. Throw away half again.
  3. Repeat until you land on the word.

Each step halves the problem: 1000 → 500 → 250 → 125 → 63 → 32 → 16 → 8 → 4 → 2 → 1. That is about 10 page-opens.

Dictionary sizeMethod A (page by page)Method B (halving)
1 000 pagesup to 1 000 looks10 looks
1 000 000 pagesup to 1 000 000 looks20 looks
1 000 000 000 pagesup to 1 000 000 000 looks30 looks

Look at the right-hand column again. The dictionary grew a million times bigger and method B needed ten extra looks. That is not a small improvement, it is a different category of solution. Noticing which category a method belongs to — before you write it, and confirmed by measurement afterwards — is what "algorithm analysis" means.

Notice what we did not do

We never mentioned how fast you flip pages, or whether you had coffee. We counted looks, not seconds. That choice is deliberate and we will come back to it properly in week 7.

Why method B needs the dictionary sorted

The halving trick only works because the words are in alphabetical order — that is what lets you throw away a whole half after a single look. Method A needs no such thing; it will find a word in a shuffled dictionary too, just slowly. Almost every dramatic speed-up in this course is paid for by some structure like this. Keep the question in mind: what does this fast method assume about its input?

1.3A first look at how work grows

Drag the slider below. Each row is a different kind of method — one that ignores the input size, one that halves like our dictionary trick, one that looks at everything once, and one that compares every item with every other item.

You do not need the names yet. The only thing to take away today: the gaps between these rows are not "a bit slower". At n = 1000 the bottom rows are already thousands of times more expensive, and they get worse from there.

1.4Correct is the floor, not the goal

When we judge a piece of code in this course, we ask three questions in order:

  1. Is it correct? Does it give the right answer for every input, including the empty and the weird ones? If not, nothing else matters.
  2. Can a human read it? Code is read far more often than it is written — by other people, and by you in three months.
  3. What does it cost? How much time does it use as the input grows, and how much memory?

Weeks 1–6 are mostly about the first two. From week 7 on, the third question takes over — and it is the one that separates a program that works on your ten test rows from one that still works on the real ten million.

1.5Counting steps by hand: a worked trace

Before a computer counts anything for us, we should be able to count by hand. Here is the number-guessing game — a friend thinks of a number from 1 to 100, and after each guess they tell you "higher", "lower" or "correct". Watch the halving method play it, step by step, when the secret number is 73.

LookRange still possibleYou guess the middleAnswer
11–10050higher
251–10075lower
351–7462higher
463–7468higher
569–7471higher
672–7473correct

Six looks, and the worst any number could take is seven. Every look throws away half of what is left: 100 → 50 → 25 → 13 → 7 → 4 → 2 → 1. The counting-up method, by contrast, could take all 100 guesses. Notice how we did the analysis: we did not run anything, we did not time anything — we simply followed the rules on paper and counted the looks. That is the same move we will automate with a computer in week 3, and it is the backbone of everything after.

The doubling rule of thumb

Here is a fact worth carrying for the whole course: each time you double the range, the halving method needs exactly one more look. 1–100 needs 7; 1–200 needs 8; 1–400 needs 9. Multiplying the size by a thousand (about ten doublings) adds about ten looks. That single sentence is why the right-hand column of the dictionary table barely moved.

1.6Setting up: Google Colab in four steps

You will not install anything. Colab is Python running on someone else's computer, displayed in your browser tab.

  1. Go to colab.research.google.com and sign in with a Google account.
  2. Choose File → New notebook.
  3. Click the grey box (a "cell") and type the line below.
  4. Press Shift + Enter to run it.
your very first cell
print("Hello, algorithms!")
Hello, algorithms!

If you see that line under the cell, you are done: you have a working Python environment and you never have to think about installation again. Rename the notebook AA_Week01.ipynb (click the title at the top left) and keep it — every week you will start a new one and keep them all in one Drive folder.

One cell can do more than one thing. Type these three lines into a new cell and run it — print shows text, but it can also do arithmetic and narrate a plan:

a second cell
print("Step 1: open the middle page")
print("Halving 1000 pages needs about", 10, "looks")
print(1000 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2 / 2)
Step 1: open the middle page Halving 1000 pages needs about 10 looks 0.9765625

That last line halved 1000 ten times in a row and landed just below 1 — the arithmetic behind "ten looks", done by the machine. We are not writing real programs yet; we are just checking that the tool obeys us. From week 2 we build up properly.

If something goes wrong

A red block under the cell is an error message, not a scolding. Read the last line first — it is usually the useful one. SyntaxError almost always means a missing quote or bracket. You will see hundreds of these; every programmer alive does.

1.7Common mistakes when you describe a method

Most first attempts at writing an algorithm fail in one of three ways. All three come from the same root — assuming the follower knows what you know. Learn to spot them on paper now and you will spot them in your code for the rest of the course.

MistakeWhat it looks likeThe fix
Ambiguous step"Sort the cards" — by what? Colour, number, size?Say exactly what to compare and what "in order" means.
No stopping rule"Keep shuffling until it looks random" — when is that?Give a condition that can be checked and will eventually be met.
Hidden assumption"Open the middle page" on an unsorted pile.State what the method needs to be true about its input.

Here is the same idea as a tiny story. You tell a friend: "To find the largest of these ten cards, look through them and pick the biggest." It sounds complete. But a truly literal follower asks: pick the biggest compared with what? The reliable version keeps a "biggest so far", starts it at the first card, and updates it only when a bigger card appears. Spelling out that hidden "so far" is the difference between a method a person can guess and one a computer can run.

A rule you can use forever

If you cannot follow your own instructions without improvising, they are not yet an algorithm. The test is boringly literal: hand them to someone (or pretend to be a machine yourself) and do exactly what is written, nothing more.

1.8Try it yourself

Task 1 — count by hand

Write ten different numbers on ten cards, face down. Your job: find the largest. You may turn over one card at a time and you must remember only one number ("the biggest so far").

How many cards do you turn over? Now answer for 100 cards, and for 1000 cards.

Show the answer

Ten cards, ten turns. Hundred cards, hundred turns. Thousand cards, thousand turns. You cannot do better: any card you never look at might be the largest. Finding a maximum in unsorted data costs one look per item — no cleverness helps. Knowing when a problem cannot be improved is as valuable as speeding one up.

Task 2 — the halving trick, by hand

A friend thinks of a number between 1 and 100. You guess; they say "higher", "lower" or "correct". Play it twice: once guessing 1, 2, 3…, once always guessing the middle of the remaining range. Count guesses both times.

What you should see

Counting up takes up to 100 guesses; halving takes at most 7 (100 → 50 → 25 → 13 → 7 → 4 → 2 → 1). Doubling the range to 200 adds exactly one guess to the halving method. In week 12 this gets a name: binary search.

Task 3 — precision practice

Write instructions for putting the letters A, C, E, B, D in alphabetical order, precise enough for someone who can only compare two letters at a time and swap them. Then follow your own instructions exactly, with no improvising. Count the comparisons.

Hint

A workable answer: "Compare the first two; if they are out of order, swap them. Move one position right and repeat to the end of the row. If you made any swap during a pass, start over from the left." That is bubble sort, and you will meet it properly — and measure how slow it is — in week 13.

Task 4 — hunt the assumption

Your friend hands you a shuffled pile of 100 numbered cards and says: "Find card number 42 by the halving trick — open the middle, then throw away a half each time." Follow the instruction literally on a shuffled pile. What goes wrong, and what one word in the instruction was doing all the work?

Show the answer

It falls apart immediately: on a shuffled pile, seeing the middle card tells you nothing about which half 42 is in, so you cannot safely throw either half away. The halving trick has a hidden assumption — the pile must be sorted. The word "middle" only helps when position tells you about value. On unsorted cards you are stuck with looking at them one by one, up to all 100. Every fast method in this course leans on some structure like this; naming that structure is half the job.

Task 5 — how many doublings?

Without a computer, work out roughly how many looks the halving method needs for a list of 1 000 000 items, using only the doubling rule of thumb from §1.5. Then do the same for 1 000 000 000. Explain your reasoning in one sentence each.

Show the answer

Doubling from 1 gets to about a million in roughly 20 steps (2 to the power 20 is just over a million), so about 20 looks. A billion is about a thousand times a million, and a thousand is about ten more doublings, so roughly 30 looks. You never had to run anything — the doubling rule turns "how big is it?" into "how many times can I halve it?", which is all binary search ever asks.

Task 6 — rewrite a bad instruction

Each of these steps breaks one of the three rules from §1.7. Say which rule, and rewrite it so a literal follower could obey it. (a) "Warm the milk a bit." (b) "Keep drawing cards until you have enough." (c) "Put the book back where it goes."

Show the answer

(a) Ambiguous — "a bit" is not checkable. Fix: "Heat the milk to 60°C." (b) No stopping rule — "enough" is never defined. Fix: "Draw cards until you hold exactly five." (c) Hidden assumption — "where it goes" assumes a known shelving scheme. Fix: "Place the book on the shelf whose label matches the first letter of the author's surname." In every case the fix replaces a word the follower would have to interpret with one they can simply act on.

1.9Self-check

Which of these is not an algorithm?

It fails two tests: the steps are not definite ("feels right" is not checkable), and there is no guarantee it ever ends.

A dictionary grows from 1 000 to 1 000 000 pages. Roughly what happens to the number of looks needed by the halving method?

Each doubling of the size adds one look. Going from 1 000 to 1 000 000 is about ten more doublings, so about ten more looks.

Why did we count "page-opens" instead of seconds?

Exactly — a step count describes the method; a stopwatch describes the method and the machine and the moment. We will use both, for different jobs.

The halving trick works on a dictionary but fails on a shuffled pile of cards. Why?

Structure buys speed. Sorted order is what lets a single look throw away half the possibilities; remove the order and the trick has nothing to stand on.

You must find the largest of 500 unsorted numbers. What is the fewest looks that could possibly work in the worst case?

Any number you never look at might be the largest, so no method can promise the answer without checking all 500. Some problems simply cannot be sped up.

1.10Homework

Due before week 2
  1. Create AA_Week01.ipynb in Colab and run a print() line that displays your name.
  2. In a text cell in the same notebook, write an everyday task of yours as a numbered algorithm (7–12 steps). Mark clearly what the input and the output are.
  3. Play the guessing game from Task 2 with a friend or with yourself and record the guess counts for both strategies in the notebook.
  4. Do the worked trace from §1.5 for your own secret number between 1 and 100. Write out the range, the middle guess and the answer at every look, and count the total.
  5. Take one instruction from your week's routine that breaks a rule from §1.7 (ambiguous, no stopping rule, or hidden assumption). Write the broken version, name the rule, and rewrite it so a literal follower could obey it.
  6. Write five sentences: which guessing strategy was better, by how much, and what you predict happens if the range goes from 1–100 to 1–1 000 000.

1.11Words from this week

TermMeaning in plain words
algorithmA finite list of clear steps turning input into output.
input / outputWhat you start with / what you end up with.
problem size (n)How big the job is: number of pages, cards, names, rows.
stepOne unit of work we have chosen to count (a page-open, a comparison, a line of code).
definite stepAn instruction with no room for interpretation — checkable, not "to taste".
doubling ruleEach time the input doubles, a halving method needs just one more step.
ColabA free browser page that runs Python for you.
cellOne box in a Colab notebook, holding either code or text.
Where this leads

You reasoned about steps on paper. Next week the computer does the counting for you — your first lines of Python.