What Is an Algorithm?
Recipes, step-by-step thinking, and why two correct methods can be wildly different.
- 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:
- Input — what you start with (tea leaves, a number, a list of names).
- Definite steps — each step is unambiguous. "Add salt to taste" is a human instruction; "add 2 grams of salt" is an algorithmic one.
- Finite — it ends. A recipe that says "stir until you feel it is right" may never end.
- Output — something comes out: a cup of tea, an answer, a sorted pile.
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.
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
- Open page 1. Is "quokka" here? No.
- Open page 2. Is it here? No.
- … 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
- Open the middle page. Are we past "quokka" alphabetically? If yes, throw away the right half; if no, throw away the left half.
- Open the middle of what is left. Throw away half again.
- 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 size | Method A (page by page) | Method B (halving) |
|---|---|---|
| 1 000 pages | up to 1 000 looks | 10 looks |
| 1 000 000 pages | up to 1 000 000 looks | 20 looks |
| 1 000 000 000 pages | up to 1 000 000 000 looks | 30 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.
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.
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:
- Is it correct? Does it give the right answer for every input, including the empty and the weird ones? If not, nothing else matters.
- Can a human read it? Code is read far more often than it is written — by other people, and by you in three months.
- 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.
| Look | Range still possible | You guess the middle | Answer |
|---|---|---|---|
| 1 | 1–100 | 50 | higher |
| 2 | 51–100 | 75 | lower |
| 3 | 51–74 | 62 | higher |
| 4 | 63–74 | 68 | higher |
| 5 | 69–74 | 71 | higher |
| 6 | 72–74 | 73 | correct |
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.
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.
- Go to colab.research.google.com and sign in with a Google account.
- Choose File → New notebook.
- Click the grey box (a "cell") and type the line below.
- Press Shift + Enter to run it.
print("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:
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)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.
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.
| Mistake | What it looks like | The 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.
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
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.
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.
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.
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.
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.
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?
A dictionary grows from 1 000 to 1 000 000 pages. Roughly what happens to the number of looks needed by the halving method?
Why did we count "page-opens" instead of seconds?
The halving trick works on a dictionary but fails on a shuffled pile of cards. Why?
You must find the largest of 500 unsorted numbers. What is the fewest looks that could possibly work in the worst case?
1.10Homework
- Create
AA_Week01.ipynbin Colab and run aprint()line that displays your name. - 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.
- Play the guessing game from Task 2 with a friend or with yourself and record the guess counts for both strategies in the notebook.
- 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.
- 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.
- 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
| Term | Meaning in plain words |
|---|---|
| algorithm | A finite list of clear steps turning input into output. |
| input / output | What you start with / what you end up with. |
| problem size (n) | How big the job is: number of pages, cards, names, rows. |
| step | One unit of work we have chosen to count (a page-open, a comparison, a line of code). |
| definite step | An instruction with no room for interpretation — checkable, not "to taste". |
| doubling rule | Each time the input doubles, a halving method needs just one more step. |
| Colab | A free browser page that runs Python for you. |
| cell | One box in a Colab notebook, holding either code or text. |
You reasoned about steps on paper. Next week the computer does the counting for you — your first lines of Python.