NOVANews
๐Ÿค– Computing

What is an algorithm?

An algorithm is a set of step-by-step instructions. A recipe is one, and so is the way you sort a hand of cards. Computers follow them exactly, which is why one tiny mistake matters.

A hand holding five playing cards fanned out in order: ace, two, three, four and five
A hand of cards sorted from ace to five. The way you sort cards is an algorithm. Photo: Ron Maijen, CC BY-SA 3.0, via Wikimedia Commons

Instructions you already follow

An algorithm is a list of steps that gets a job done or solves a problem. The steps are clear, they happen in a set order, and they come to an end. It sounds like a fancy computer word, but you use algorithms every day.

A recipe is an algorithm. To make pancakes, you measure the flour, crack in an egg, pour in the milk, whisk, then cook each side. The order matters. You cannot flip a pancake before you have poured it into the pan!

Getting ready for school is an algorithm too. So are directions to a friend's house, the rules of a board game and the steps for a long division sum. Whenever you follow steps in order to reach a goal, you are running an algorithm.

A word from Baghdad

The word "algorithm" comes from the name of a mathematician, Muhammad ibn Musa al-Khwarizmi. He worked in Baghdad, in what is now Iraq, more than 1,100 years ago, at a famous centre of learning called the House of Wisdom. He wrote about the Hindu-Arabic numerals, the digits 0 to 9 that we still use today.

Much later, in 1843, Ada Lovelace published notes about a calculating machine called the Analytical Engine, designed by the English inventor Charles Babbage. She explained how the machine could be given step-by-step instructions. Many people consider her notes to include the first ever computer program.

Sorting a hand of cards

Think about how you sort a hand of cards. Many people pick up one card at a time and slide it into the right place among the cards they already hold. Then they pick up the next card and do the same, until every card is in order. That is a sorting algorithm.

Here is a different one. Spread the cards on a table. Find the smallest card and put it first. Find the next smallest and put it second. Keep going until none are left. Both algorithms do the same job, but in different ways.

For five cards, either way is quick. But computers often sort millions of things, like names, scores or prices. So computer scientists invent and compare algorithms to find ones that finish with the fewest steps.

The guessing game

Play this with a friend. They think of a number from 1 to 100, and you guess. After each guess, they say "higher" or "lower".

One algorithm is to guess 1, then 2, then 3, and so on. That is called a linear search. It works, but it could take 100 guesses. A smarter algorithm is to guess 50 first. Whatever the answer, you have ruled out half the numbers. Then guess halfway through the half that is left, and keep halving. This is called binary search, and it never needs more than 7 guesses.

Binary search is amazingly fast. With a thousand numbers, you need at most 10 guesses. With a million numbers, you need at most 20. But it only works when things are in order, like the words in a dictionary.

Computers do exactly what they are told

A computer program is an algorithm written in a language a computer understands. Computers are incredibly fast, but they cannot guess what you meant. They follow every instruction exactly, even a wrong one.

A mistake in a program is called a bug, and fixing it is called debugging. On 9 September 1947, people testing the Mark II computer at Harvard University found a real moth stuck inside it. They taped the moth into their logbook with the note "First actual case of bug being found." The joke worked because engineers already called faults bugs.

Even Ada Lovelace was a debugger. Charles Babbage later wrote that she had found a "grave mistake" in some of his working. Today, algorithms choose the fastest route in a maps app and help decide which videos you see next. They are written by people, so it is always worth asking: what steps is this algorithm following?

๐Ÿ“š Words to know

Algorithm
A set of clear, step-by-step instructions for doing a job or solving a problem.
Program
An algorithm written in a language that a computer can follow.
Bug
A mistake in a program that makes it do the wrong thing.
Debugging
Finding and fixing the mistakes in a program.
Binary search
A way of searching an ordered list by checking the middle and throwing away half each time.

๐Ÿงช Try this: program a human robot

  1. Secretly draw a simple picture made only of straight lines, such as a house made from a square with a triangle on top. Keep it hidden.
  2. Write step-by-step instructions for drawing it, for example "Draw a line 5 centimetres long across the middle of the page." Do not say what the picture is.
  3. Give your instructions to a partner, who must follow each step exactly as written, like a computer. No guessing and no questions allowed!
  4. Compare the two pictures. Anything that came out different is a bug. Debug your instructions, then try again with someone new.
  5. Bonus: shuffle ten playing cards and sort them using the "slide each card into its place" algorithm. Count how many moves it takes.

๐Ÿง  Quick check

4 questions
1 What is an algorithm?
2 Using the "higher or lower" halving method, what is the most guesses you need to find a number from 1 to 100?
3 Why does binary search only work on a list that is in order?
4 What is a bug in a computer program?

Where this comes from

We check our facts against trusted sources. Want to find out more? Try these:

Keep exploring