Algorithms: step-by-step instructions
An algorithm is a clear list of steps that solves a problem. Learn sequence, choice and repetition, and a guessing trick that is always fast.
Imagine you have to teach someone to make a cup of tea. You would not just say "make tea". You would give them steps: boil the water, put a tea bag in a cup, pour the water, wait, take the bag out. That list of steps is an algorithm.
What is an algorithm?
An algorithm is a clear list of steps that solves a problem. Recipes, directions to a friend's house, and the rules for a board game are all algorithms.
A good algorithm has three things:
- Each step is clear. "Add a bit of salt" is vague. "Add one teaspoon of salt" is clear. A computer cannot guess what you meant.
- Each step is simple. Big jobs are broken down into small ones.
- It finishes. It has an end. An algorithm that never stops is a bug.
Computers need algorithms for everything they do. The computer's instructions are an algorithm written down very carefully.
Three building blocks
Almost every algorithm is built from just three ideas:
- Sequence. Do steps one after another, in order.
- Choice. Look at something, and do one thing if it is true and another thing if it is not. ("If the water is boiling, pour it.")
- Repetition. Do a step again and again, until a rule says to stop. ("Keep stirring until the sugar has melted.")
An example: find the biggest number
Say you have a list of numbers, such as 4, 9, 2, 7. How would you find the biggest? You would probably just look. But a computer has to look at one at a time, so it needs steps:
Let us follow the steps with 4, 9, 2, 7:
| Number we look at | Is it bigger than the biggest so far? | Biggest so far |
|---|---|---|
| 4 (the first) | (we start here) | 4 |
| 9 | Yes | 9 |
| 2 | No | 9 |
| 7 | No | 9 |
No numbers are left, so the answer is 9. The same algorithm works for a list of three numbers or three million. That is the power of an algorithm: you write it once, and it works every time.
A faster way: the guessing game
Some algorithms are much faster than others. Try this game: a friend thinks of a number from 1 to 100. You must find it by guessing, and your friend says "higher" or "lower".
Slow algorithm: guess 1, then 2, then 3, and so on. In the worst case, you need 100 guesses.
Fast algorithm: always guess the middle of what is left.
- Guess 50. "Higher." Now it must be 51 to 100.
- Guess 75. "Lower." Now it is 51 to 74.
- Guess 62. "Higher." Now 63 to 74.
- And so on. Each guess cuts the possibilities in half.
With this method, you need at most 7 guesses for any number from 1 to 100. For 1 to 1,000 it takes at most 10. For a number between 1 and a million, at most 20. That is incredibly fast, and it is used all the time inside computers to look things up in huge lists. It is called binary search.
Two algorithms can both give the right answer, but one may take a second and the other a year. Computer scientists spend a lot of time looking for faster algorithms.
Algorithms all around us
- A sat-nav uses an algorithm to find the shortest route.
- A search engine uses algorithms to rank pages.
- A video app uses algorithms to suggest what to watch next.
- When an AI learns, it also follows an algorithm: a simple loop, repeated many times. You will see that in Train the Puppy: pick a move, get a reward, update the notes, repeat.
Try it without a computer
Write an algorithm for tying a shoelace or making a sandwich. Then ask a friend to follow it exactly, step by step, doing only what the words say. You will soon find the steps you forgot, which is exactly what programmers experience every day.
Words to know
- Algorithm
- A clear list of steps that solves a problem.
- Sequence
- Steps done one after another.
- Choice
- Doing one thing if something is true, and another if it is not.
- Repetition
- Doing a step again and again until a rule says to stop.
- Binary search
- Finding something by always checking the middle of what is left, which halves the possibilities.
Check yourself
What are the three building blocks of algorithms?
Show the answer
Sequence (steps in order), choice (if this, then that) and repetition (doing steps again and again).
What is the biggest number in the list 3, 8, 1, 6?
Show the answer
8.
Using the halving method, what is the most guesses you would need to find a number between 1 and 1,000?
Show the answer
10 guesses.
Why must the steps in an algorithm be clear?
Show the answer
A computer cannot guess what you meant. It does exactly what the steps say.