What are Algorithms?
The word Algorithm tends to suggest something complicated, obscure or even magical but actually they are a common part of everyday life. Some are complex and some are very simple.
An Algorithm is a set of precise step by step instructions to achieve a task or solve a problem. These instructions must be complete and unambiguous such that they can be followed without any additional knowledge, or abilities such ‘common sense’ or intuition. (Which, for a computer, makes them ideal!)
In computing they are used in vitually all applications including the highly sophisticated algorithms behind search engines like Google, shopping sites like Amazon and social media like Facebook.
Algorithms are certainly an important part of creating computer programmes but they also figure in many other areas. A cooking recipe is a good example. The recipe sets out the ingredients needed, their quantities, the steps to assemble the ingredients and the way they need to be cooked in order to produce the specified dish. One person can write the recipe and other people can independently follow the instructions to create exactly the same dish.
[As an aside, algorithms got a bad press, and most of the blame, for the exam results fiasco in Summer 2020 that affected the lives of so many young people. But it was nonsense to hold a 'rogue' algorithm responsible - the fault lay entirely with those writing and relying on the algorithm. A bit like me writing a cake receipe and then when the cake tastes awful, blaming the receipe!]
Algorithms are not computer code
You might be thinking that algorithms and computer code are the same thing, but actually they are different concepts with different purposes. Algorithms are written in English so that humans can understand them without needing to know any computer code. This makes them easier to write in the first place. And easier for people to examine the algorithm, test the logic and if necessary make changes.
Computers cannot read algorithms. Once the algorithm for a particular task has been finalised the step by step instructions need to be converted into computer code (such as Scratch or Python) and then that code is read and processed by the computer. Computer code is more complex and more precise than an algorithm.
If we missed out the step of writing an algorithm and went straight to writing computer code, particularly for complex tasks, the code is likely to contain errors and inefficiencies that can be difficult to identify and correct. It is usually easier to spot errors (or ‘bugs’) when the logic is in plain English.
So the algorithm can be seen as a ‘halfway house’ between the problem and the computer code to solve that problem.
Getting started with Algorithms
Children first meet algorithms as early as their first or second year in school. These will be part of ‘unplugged activities’ where children learn about some aspects of computer coding without actually using computers.
To give an idea of how this works here is an algorithm that might be created by children for brushing their teeth.
How to brush your teeth
- Take cap off tube of toothpaste
- Pick up toothbrush from holder
- Wet toothbrush using water from tap
- Put pea-sized amount of toothpaste on bristles of toothbrush
- Brush bottom teeth for 2 minutes
- Brush top teeth for 2 minutes
- Spit out toothpaste
- Rinse toothbrush with water from tap
- Replace toothbrush in holder.
- Replace cap on tube of toothpaste
Other examples might be Getting Ready for School in the Morning or The Daily Routine for looking after a pet.
Or, How to make a jam sandwich ...
One popular lesson in primary schools involves the children writing instructions (the algorithm) to tell the teacher how to make a jam sandwich. The teacher, armed with the props of jar of jam, bread, butter, plate etc, then attempts to follow the instructions exactly as they are written.
The results usually include the teacher responding to an instruction like ‘Put the jam on the bread.’ by putting the jar of jam on top of the loaf. Lots of fun is had by all and the children can see the need to be very careful in stating the instructions. This is certainly a game you can try at home!
Children often use beebots and other floor robots in their early years at school. These robots are programmed to follow a sequence of commands using buttons to record each command. There are therefore an excellent introduction to using algorithms. See our guide to floor robots for more information.
As they progress through school, children move on to learning how to use an algorithm as the first step in creating computer code. A simple example of this would be working on a computer game and wanting to animate a character to move around a square with each side of the square being 10 steps long.
If our algorithm simply stated “Move around a square”, there would be insufficient instructions for a robot to perform the task or indeed for a human who does not understand the concept of a square. Also we have not said how big the square needs to be.
A much better way of stating the instructions would be as follows.
1. Move forward 10 steps
2. Turn 90 degrees right
3. Move forward 10 steps
4. Turn 90 degrees right
5. Move forward 10 steps
6. Turn 90 degrees right
7. Move forward 10 steps
8. Turn 90 degrees right
This should work okay but if we review the above instructions we can see there is a lot of repetition. We can make our algorithm more concise, and better, by using a ‘repeat’ instruction. So that it now looks like this.
1. Repeat 4 times:
1.1. Move forward 10 steps
1.2. Turn 90 degrees right
And if we now code this in Scratch our program would look like this.
This process of identifying ways to make them more concise, so that they use less code in the computer programme and therefore run faster, is an important part of writing a good algorithm.
A flowchart is the graphical or pictorial representation of an algorithm and the two are often used together.
The flowchart uses symbols, shapes and arrows in order to show the steps needed for a process or a program rather than words. The standard shapes that are used are: an oval for Start and End; a rectangle for a mathematical operation or a process; a diamond for a logical decision and a parallelogram to indicate input and output.
Here is a simple example of a flowchart to demonstrate the steps needed for a ‘frost guard’ that sounds an alarm if the temperature is below 32°F.
The advantage of using flowcharts is that, because they use clear English and easy-to-understand standard symbols, they can be created and understood by people without specialist computer coding knowledge. This means a team of people could analyse a particular problem, identify the steps needed to create a solution and record this as a flowchart. The flowchart can be reviewed for errors in logic etc before being passed to a coder to create the computer code.
Once the code has been written if there are errors or bugs one can return to the flowchart to help identify the problems without pouring over the detailed code.
Algorithmic thinking is the ability to define the precise steps that are needed to solve a problem. Using it allows people to break down problems and identify effective solutions in terms of discrete steps.
It is a skill that is crucial in subjects like Maths and Science as well as Coding but actually is relevant in many situations and indeed at any age.
Algorithms in the curriculum
Algorithms feature in the requirements of most national curricula. For example the National Curriculum for England and Wales states:
Key Stage1: Pupils should be taught what algorithms are and how they are implemented as programs on digital devices.
Key Stage2: Pupils should be taught to use logical reasoning to explain how simple algorithms work and to detect and correct errors in algorithms and programs.
Key Stage 3: Pupils should be able to understand several key algorithms that reflect computational thinking (for example ones for sorting and searching); use logical reasoning to be able to compare the utility of alternative algorithms for the same problem.
How you can help
Just like all skills, kids can improve their algorithmic thinking through regular practice. And it can be good fun!
Encourage your child to create algorithms to solve everyday problems or to give instructions for everyday tasks. For example:
Getting ready for school
Some tips for helping with algorithms:
- Challenge your child to be as clear and unambigious with the instructions as possible.
- Each step should be a discrete instruction rather than combining several instructions. This is because when we write the code based on the algorithm we will need to deal with each instruction separately.
- Computers don’t understand your intentions and they have no common sense. So, for example, if you don’t specify that you need to put your socks on before you put your shoes on, you may end up looking silly!
- The instructions must be in the right order (the order of steps is called a sequence) Putting the cake in the oven to bake for 20 minutes before the oven has been turned on won’t work!
- There can be many different algorithms for the same problem, they may all work but some are much better than others!
Once your child has written the algorithm you can play the part of the ‘dumb’ robot that attempts to follow the instructions exactly as they are written. (see the Jam Sandwich above!)
Without knowing it, they’ll be exploring important coding concepts like repetition (eat a spoonful of cereal until bowl is empty), sequencing (put socks on and then put shoes on), and conditional logic (if raining put on coat).