Watch on Instagram

Code Bytes · "Start Here: What Is Coding?" · Episode 6 · Thinking like a coder

Algorithms in real life

An algorithm is a set of clear steps that solves a problem, in order, and then stops. Recipes, directions and laundry are everyday algorithms, and coders use one to find the tallest person in a line, which takes six short lines of Python.

Posted · About a 1 minute watch, 4 minute read · Take the quiz

Recipe = program?

A recipe and a computer program have something in common: both are algorithms. An algorithm is a step-by-step procedure that solves a problem in a limited number of steps. Three things make it work:

  • Clear steps: each step says exactly what to do.
  • In order: the steps happen one after another.
  • It stops: when the last step is done, the job is finished. An algorithm that never ends doesn't solve anything.

You already use them

You follow algorithms every day without calling them that:

  • Directions to a friend's house: turn left, go two blocks, turn right.
  • Tying your shoes: cross the laces, loop, pull tight.
  • Doing laundry: sort, wash, dry, fold.

One difference: real-life instructions can be a bit vague ("add a pinch of salt"), and people fill the gaps. A computer can't, so a computer's algorithm has to be completely exact.

An algorithm coders use: find the tallest

Here's a classic. You want the tallest person in a line:

  1. Look at the first person and remember them.
  2. Look at each next person.
  3. If they're taller than the one you remember, remember them instead.
  4. At the end, the one you remember is the tallest.

Try it with five people who are 150, 172, 165, 180 and 158 cm tall. You start by remembering 150. Then 172 is taller, so you remember 172. 165 isn't taller, so nothing changes. 180 is taller, so you remember 180. 158 isn't taller. The line is done: the tallest is 180 cm.

The same steps in Python

The steps are clear enough for a computer. In Python, they take just six short lines:

``python heights = [150, 172, 165, 180, 158] tallest = heights[0] for h in heights: if h > tallest: tallest = h print(tallest) ``

Output:

`` 180 ``

You don't need to understand every symbol yet (lists, for loops and if come later in Code Bytes), but you can already match the lines to the steps:

  • heights = [...] is the line of people (their heights in centimetres).
  • tallest = heights[0] remembers the first person (Python counts from 0, so heights[0] is the first one).
  • for h in heights: looks at each person in turn.
  • if h > tallest: and tallest = h mean "if they're taller, remember them instead".
  • print(tallest) shows the one you remember at the end.

Same idea, bigger

Map apps finding a route and search engines ranking results run algorithms too. Theirs are far bigger and cleverer (shortest-path and ranking methods), but the idea is the same: clear steps that solve a problem and then stop.

Try it yourself

Open any free online Python editor (search "online Python") and paste the code above. Then change it to find the shortest person instead: rename tallest to shortest and flip > to <:

``python heights = [150, 172, 165, 180, 158] shortest = heights[0] for h in heights: if h < shortest: shortest = h print(shortest) ``

It should print 150. Now add your own family's or friends' heights to the list and run it again.

Good to know

  • The code is Python 3. Every snippet on this page was run, and the outputs shown are copied from that run.
  • The definition follows Britannica (a procedure that solves a problem in a finite number of steps) and Khan Academy (a sequence of steps to solve a problem). "And then stop" stands for that "finite number of steps".
  • Recipes are a common teaching comparison, but real recipes can be vaguer than a computer algorithm is allowed to be.
  • "Just much bigger ones" is a simplification: map routing and search ranking use far more complex algorithms than this one.
  • Lists and loops get their own episodes later; here the code is a first glimpse, not a full explanation.

Answer to the comment question

With this algorithm, how many people must you look at in a line of fifty? 50, all of them. The algorithm can't know who is tallest until it has seen everyone: the very last person could be the tallest of all. (The code above even "looks at" the first person twice: it remembers them, then the loop compares them with themselves.)

49 is also right, if you count comparisons instead of people looked at: after remembering the first person, you compare each of the other 49 people with the one you remember, so there are 49 "is this one taller?" checks. Either way, the work grows with the length of the line: twice as many people means about twice as much looking.

Test your knowledge

Quick quiz: Algorithms in real life

4 multiple-choice questions. Pick one answer for each, then check how you did.

1What is an algorithm?
2Which of these is an everyday algorithm?
3Finding the tallest in 150, 172, 165, 180, 158: who do you remember after looking at 165?
4In the Python version, what does if h > tallest: check?

Sources

Video transcript

What does a recipe have in common with a computer program?

Both are algorithms: clear steps that solve a problem, in order, and then stop.

You use them daily. Directions to a friend's house. Tying your shoes. Doing laundry.

Here's one coders use: find the tallest person in a line. Look at the first person and remember them. Then look at each next person. If they're taller, remember them instead. At the end, the one you remember is the tallest.

The steps are clear enough for a computer. In Python, it's just six short lines.

Map apps and search engines run algorithms too, just much bigger ones.

With this algorithm, how many people must you look at in a line of fifty? Comment your answer below!

That's your byte.

More from "Start Here: What Is Coding?"

All "Start Here: What Is Coding?" lessons