Algorithms: step-by-step instructions

Beginner6 min readPart 6 of 9

An algorithm is a clear list of steps that solves a problem. Learn sequence, choice and repetition, and a guessing trick that is always fast.

Imagine you have to teach someone to make a cup of tea. You would not just say "make tea". You would give them steps: boil the water, put a tea bag in a cup, pour the water, wait, take the bag out. That list of steps is an algorithm.

What is an algorithm?

An algorithm is a clear list of steps that solves a problem. Recipes, directions to a friend's house, and the rules for a board game are all algorithms.

A good algorithm has three things:

Computers need algorithms for everything they do. The computer's instructions are an algorithm written down very carefully.

Three building blocks

Almost every algorithm is built from just three ideas:

  1. Sequence. Do steps one after another, in order.
  2. Choice. Look at something, and do one thing if it is true and another thing if it is not. ("If the water is boiling, pour it.")
  3. Repetition. Do a step again and again, until a rule says to stop. ("Keep stirring until the sugar has melted.")

An example: find the biggest number

Say you have a list of numbers, such as 4, 9, 2, 7. How would you find the biggest? You would probably just look. But a computer has to look at one at a time, so it needs steps:

Startbiggest = first numberlook at the next numberbigger?yesbiggest = this onenoany left?yesnoanswer: biggest
An algorithm as a flowchart. Rectangles are steps, diamonds are questions, and the loop on the left is a repeat.

Let us follow the steps with 4, 9, 2, 7:

Number we look atIs it bigger than the biggest so far?Biggest so far
4 (the first)(we start here)4
9Yes9
2No9
7No9

No numbers are left, so the answer is 9. The same algorithm works for a list of three numbers or three million. That is the power of an algorithm: you write it once, and it works every time.

A faster way: the guessing game

Some algorithms are much faster than others. Try this game: a friend thinks of a number from 1 to 100. You must find it by guessing, and your friend says "higher" or "lower".

Slow algorithm: guess 1, then 2, then 3, and so on. In the worst case, you need 100 guesses.

Fast algorithm: always guess the middle of what is left.

With this method, you need at most 7 guesses for any number from 1 to 100. For 1 to 1,000 it takes at most 10. For a number between 1 and a million, at most 20. That is incredibly fast, and it is used all the time inside computers to look things up in huge lists. It is called binary search.

Why speed matters

Two algorithms can both give the right answer, but one may take a second and the other a year. Computer scientists spend a lot of time looking for faster algorithms.

Algorithms all around us

Try it without a computer

Write an algorithm for tying a shoelace or making a sandwich. Then ask a friend to follow it exactly, step by step, doing only what the words say. You will soon find the steps you forgot, which is exactly what programmers experience every day.

Words to know

Algorithm
A clear list of steps that solves a problem.
Sequence
Steps done one after another.
Choice
Doing one thing if something is true, and another if it is not.
Repetition
Doing a step again and again until a rule says to stop.
Binary search
Finding something by always checking the middle of what is left, which halves the possibilities.

Check yourself

  1. What are the three building blocks of algorithms?

    Show the answer

    Sequence (steps in order), choice (if this, then that) and repetition (doing steps again and again).

  2. What is the biggest number in the list 3, 8, 1, 6?

    Show the answer

    8.

  3. Using the halving method, what is the most guesses you would need to find a number between 1 and 1,000?

    Show the answer

    10 guesses.

  4. Why must the steps in an algorithm be clear?

    Show the answer

    A computer cannot guess what you meant. It does exactly what the steps say.