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 conditionWhat fails without it
Every step is clearIt cannot be carried out without deciding yourself
The sequence terminatesThe program runs forever
It solves every caseIt 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

  1. Whoever carries it out must decide what “best” means.
  2. Two people will get different results.
  3. So it is not a step but an aim.
  4. A step would be: “compare each with the current maximum”.

Example 2 — why an empty list breaks things

  1. An algorithm to find the largest number in a list.
  2. It starts from the first item as the maximum.
  3. In an empty list there is no first item.
  4. So it must be decided in advance what to return then.
Sorting: Which condition of an algorithm does the instruction break? — 6 items into 3 groups (A step is not clear, The sequence does not terminate, It breaks on an edge case), with a check and a reason for each item.