Grade 7 Computer Science · What a program is
What an algorithm is
A sequence of steps that terminates, each of them clear, solving the problem in every case.
Instructions that work on every list but one
Instructions for finding the largest number in a list look perfect and work on every list you try — almost. There is one input, perfectly legal, that brings them down. What is it, and why is it the one everybody forgets?
The word sounds grand, and the definition is simple. An algorithm is a recipe: a sequence of steps leading from an input to an output. What distinguishes an algorithm from a general idea is three conditions that can be checked one by one.
| The condition | What fails without it |
|---|---|
| Every step is clear | It cannot be carried out without deciding yourself |
| The sequence terminates | The program runs forever |
| It solves every case | It works on the example and breaks on an edge case |
Edge cases — where algorithms break
An edge case is a legal input sitting at the edge of what you had in mind: zero, an empty list, a single item, the largest number, text with no letters. They are legal, so the program must handle them, and they are rare, so one forgets to check them. A good habit is to ask three questions of every algorithm: what happens when there is nothing, what happens when there is exactly one, and what happens when everything is the same.
Example 1 — why “find the best one” is not a step
- Whoever carries it out must decide what “best” means.
- Two people will get different results.
- So it is not a step but an aim.
- A step would be: “compare each with the current maximum”.
Example 2 — why an empty list breaks things
- An algorithm to find the largest number in a list.
- It starts from the first item as the maximum.
- In an empty list there is no first item.
- So it must be decided in advance what to return then.