Definition
Write instructions for making a cup of tea so precise that a robot who has never seen a kitchen could follow them. It is harder than it sounds.
An algorithm is a precise, ordered set of steps for solving a problem or completing a task. A good algorithm is clear, has a definite start and end, handles the situations it might meet, and always gives the right result when followed exactly.
In simple words
A recipe is an algorithm. So are the directions to a friend's house, the steps for washing your hands and the rules for long division. Each tells you exactly what to do, in what order, until the job is done.
Why it matters
Everything a computer does, it does by following algorithms. AI systems use algorithms to learn from data and to make predictions. Learning to write clear algorithms trains you to think precisely, and to notice when instructions are vague or incomplete.
Key terms
- Algorithm
- A precise, ordered set of steps to solve a problem.
- Input
- The information an algorithm starts with, such as the ingredients or the numbers to add.
- Output
- The result an algorithm produces.
- Sequence
- Steps carried out one after another in a fixed order.
- Selection
- A step that chooses what to do based on a condition, such as "if the water is boiling".
- Repetition
- Repeating steps until a condition is met, such as "stir until dissolved".
Explained in detail
What makes a good algorithm
| Property | Meaning | Bad example |
|---|---|---|
| Precise | Every step has only one meaning | "Add some sugar" |
| Ordered | Steps are in the right sequence | "Pour the tea" before "boil the water" |
| Finite | It finishes after a limited number of steps | "Keep stirring" with no end |
| Complete | It handles every situation it may meet | No step for "if there is no milk" |
| Correct | It always gives the right output | Long division that forgets to carry |
The three building blocks
Almost every algorithm is built from three simple ideas. Sequence: do this, then this, then this. Selection: if something is true, do one thing, otherwise do another. Repetition: keep doing something until a condition is met. A tea-making algorithm uses all three: boil water, add tea leaves (sequence); if someone wants sugar, add one spoon (selection); stir until the colour is dark brown (repetition). With just these three blocks, any procedure a computer carries out can be described.
Different algorithms, same answer
Often there is more than one algorithm for a problem, and some are much faster. To find a name in a register of 1,000 students, you could check every name from the top: up to 1,000 checks. If the register is in alphabetical order, you can open it in the middle, see whether the name comes before or after, and throw away half each time. That takes at most about ten checks. Computer scientists compare algorithms by how much time and memory they need, which matters enormously when the data has millions of items.
Algorithms all around us
- A maps app uses an algorithm to find the shortest route between two places.
- A video app uses an algorithm to choose which video to recommend next.
- Railway reservation systems use algorithms to assign seats and waiting-list positions.
- Machine learning uses algorithms that adjust a model step by step to reduce its mistakes.
The word itself comes from the name of the ninth-century Persian mathematician al-Khwarizmi, whose books on arithmetic helped spread the decimal number system, which had developed in India, to the rest of the world.
Writing an algorithm is also a test of understanding. If you cannot write clear steps for something, you probably do not fully understand it yet.
How it works
- State the input and the output you want.
- Write the steps in order, one action per step.
- Replace vague words such as "some" or "a bit" with exact amounts.
- Add "if" steps for different situations and "repeat until" steps where needed.
- Test it by having someone follow it exactly, word for word.
Try it yourself
The very literal robot
Meera writes instructions for making a sandwich and her friend Kabir acts as a robot who follows them exactly.
Put the hand-washing algorithm in order
Arrange the steps correctly.
- 1Dry with a clean towel
- 2Rub hands together for at least 20 seconds
- 3Apply soap
- 4Wet your hands with clean water
- 5Rinse under running water
Common mistakes
- Using vague words like "some" or "a little".
- Forgetting what to do in unusual situations.
- Writing a repeat with no way to stop.
Check yourself
Summary
- An algorithm is a precise, ordered, finite set of steps.
- Sequence, selection and repetition are its building blocks.
- Some algorithms are much faster than others for the same problem.
Finished reading?
Sources: BBC Bitesize: Introduction to computational thinking · CS Unplugged