Imagine you want to make toast. You pick up bread, put it in the toaster, wait, and take it out. Those steps are an algorithm.

An algorithm is a list of steps that solves a problem. Computers need these steps to be very clear because they cannot guess what we mean.

A recipe for a computer

Let’s tell a robot how to find the biggest number in a bag. First, the robot looks at one number and calls it the biggest. Then it checks every other number. If it finds a bigger one, that becomes the new biggest. At the end, the robot shows us the answer.

  • Start with the first number.
  • Look at the next number.
  • Keep the bigger one.
  • Repeat until the bag is empty.
  • Return the biggest number.

Try it with 3, 8, 2, and 5. We keep 3 first. Then 8 beats 3. The numbers 2 and 5 do not beat 8. So 8 is the answer.

Find the biggest numberPython
numbers = [3, 8, 2, 5]
biggest = numbers[0]

for number in numbers[1:]:
    if number > biggest:
        biggest = number

print(biggest)  # 8

What makes an algorithm good?

A good algorithm has a clear start, a clear finish, and steps that are easy to follow. It should also give the right answer for many different inputs, not only one lucky example.

There can be many algorithms for the same job. You can walk to a toy by going around the table or under it. Both paths may work, but one can be shorter or safer.

Your tiny challenge

Write steps for brushing your teeth. Give them to a friend and ask them to follow the steps exactly. If a step is confusing, make it clearer. That is how programmers improve algorithms: write, test, notice, and fix.

Remember: an algorithm is not magic. It is simply a careful path from a question to an answer.