fixmint
EN

What is an algorithm?

The textbook saysAlgorithm: a finite, well-defined sequence of instructions for solving a class of problems or performing a computation, characterised by its correctness and efficiency.

In plain wordsA list of clear steps, done in order, that gets a job done every time.

Making a peanut butter sandwich is an algorithm, and you have probably done one. Get two slices of bread. Open the jar. Spread peanut butter on one slice. Put the other slice on top. Done. The steps are clear, they come in the right order, and anyone who follows them gets a sandwich.

That is all an algorithm is: a list of exact steps to solve a problem. A recipe is one. So are directions to a friend's house, and the rules for tying a shoelace.

Computers are very good at following algorithms, because they do exactly what they are told, very quickly, and never get bored. But they cannot guess what you meant. Each step has to be spelled out.

Good algorithms also have two special moves: choosing ('if the water is boiling, add the pasta') and repeating ('stir until it is smooth').

Here is one you can try. A friend picks a number from 1 to 100, and you must guess it. They only say 'higher' or 'lower'.

  • You could guess 1, then 2, then 3. That works, but it might take 100 tries.
  • Or guess the middle, 50. If they say 'higher', you now know it is between 51 and 100, so you guess the middle of that, 75. Every guess throws away half of the possibilities.

The second way never needs more than 7 guesses for 100 numbers. Both ways are algorithms that solve the same problem, but one is much quicker. That is why people who write programs care about finding good ones.

Algorithms are all around you. A map app uses one to find the shortest route. A music app uses one to pick the next song. A search engine uses one to put results in order. Sorting a pile of cards from smallest to largest is another, and there are many ways to do it.

The word comes from the name of al-Khwarizmi, a scholar who worked in Baghdad in the 800s and wrote about step-by-step methods in mathematics.

Picture it

Picture a card game where you must put a messy hand of cards in order. You could pick up each card and slide it into its place among the ones already sorted. That plan, followed the same way each time, is an algorithm, and it works for five cards or fifty.

Words you might hear

Step
one small clear action in a list of actions
Program
a set of instructions written so that a computer can follow them
Sorting
putting things in order, like smallest to largest or A to Z

One step further

Computer scientists compare algorithms by how their work grows as the job gets bigger. Guessing a number by halving needs about one more guess each time the range doubles, so even a range of a billion needs only around 30. Counting up one by one would need up to a billion. The first approach is called binary search. Many modern apps combine algorithms with data, so what you see can depend on what the program has learned about people like you, which is one reason algorithms are discussed in public as well as in classrooms.

Next questions

  • What is artificial intelligence?

    Computer programs that learn from lots of examples, instead of following rules that a person wrote out step by step.

  • How does the internet work?

    Millions of computers joined by cables and radio waves, passing tiny pieces of messages to each other until they arrive.

  • What is encryption?

    Scrambling a message with a secret rule so that only someone who knows the rule can unscramble and read it.

Sources

Questions

Is an algorithm the same as a computer program?

Not quite. An algorithm is the idea, the steps. A program is that idea written in a language a computer can run. The same algorithm can be written as programs in many languages.

Do algorithms only exist in computers?

No. Recipes, knitting patterns and directions are algorithms for people. The word became famous because computers rely on them, but the idea is much older.

Can an algorithm be unfair?

Yes. An algorithm follows its steps, but people choose the steps and the data. If these include unfair patterns, the results can carry them too, so people check how algorithms are used.