A definition you can use
An algorithm is a finite list of clear steps that, when followed in order, solves a problem or completes a task. The word is often associated with computers, but the idea is much older and much more ordinary. Long division, the method taught in school for dividing large numbers, is an algorithm. So is the set of directions you give a friend to reach your house.
What makes something an algorithm rather than just advice is precision. Each step must be clear enough that whoever follows it does not need to guess.
Recipes as algorithms
A recipe is a familiar example. Consider a simple way to make a cheese sandwich:
- Place two slices of bread on a plate.
- Spread butter on one side of each slice.
- Put a slice of cheese on one buttered side.
- Place the other slice on top, buttered side down.
- Cut the sandwich in half.
The recipe has inputs (bread, butter, cheese), a sequence of actions and an output (the sandwich). It ends after a fixed number of steps.
Now imagine giving the instruction “make a sandwich” to someone who takes every word literally. They might not know whether to butter the outside of the bread, how much cheese to use or where to put the knife. A famous classroom exercise has students write instructions for a sandwich while another person follows them exactly, with funny results. The lesson is that everyday instructions rely on shared understanding, and a computer has none unless you provide it.
Four qualities of a good algorithm
- Clear steps. Each instruction has one interpretation. “Add a bit of salt” is vague; “add one teaspoon of salt” is clear.
- Defined order. The sequence is specified, including any decisions, such as “if the mixture is too thick, add water”.
- Finite. It must come to an end. A procedure that runs forever never gives an answer.
- Produces a result. For valid inputs, it should reliably solve the stated problem.
The decisions and repetitions inside an algorithm are exactly what conditions and programming loops express in code.
Example: finding the largest number
Suppose you have a list of numbers: 7, 3, 12, 5, 9. To find the largest:
- Take the first number, 7, as the largest so far.
- Look at the next number, 3. It is not bigger than 7, so keep 7.
- Next is 12. It is bigger than 7, so now 12 is the largest so far.
- Next is 5. Keep 12.
- Next is 9. Keep 12.
- No numbers remain. The answer is 12.
Notice that you never needed to see the whole list at once or to think cleverly. The method is mechanical, and it works for any list, however long. Writing a method that works for every possible input, and not only for the example, is the heart of the algorithmic approach.
Example: a guessing game
Here is a more interesting algorithm. Someone picks a whole number from 1 to 100 and you must find it, being told only “higher” or “lower” after each guess.
A poor method is to guess 1, then 2, then 3. In the worst case this takes 100 guesses.
A better method is to always guess the middle of the remaining range:
- Guess 50. If told “higher”, the number must be in 51 to 100. If “lower”, in 1 to 49.
- Guess the middle of what remains, and repeat.
Each guess cuts the remaining possibilities roughly in half: 100 becomes at most 50, then 25, then 13, then 7, then 4, then 2, then 1. That is at most seven guesses. We can double check this with powers of two. Two to the power 6 is 64, which is less than 100, so six guesses are not always enough. Two to the power 7 is 128, which is at least 100, so seven always suffice.
The two methods are both correct, since they both find the number, but one is far more efficient. This is the reason people study algorithms: not only to get an answer, but to get it using fewer steps, less time or less memory. The halving idea is known as binary search when applied to sorted data on a computer.
Representing algorithms
Algorithms can be written in several ways:
Plain language steps (as above), flowcharts with boxes and arrows, pseudocode that looks like code but is not tied to any language, and real program code. Starting in plain language is usually best: if you cannot describe the steps in words, writing code will not rescue you.
Algorithms in everyday technology
Algorithms operate behind many tools. A search engine uses them to order results, a map application uses them to choose routes and a music service uses them to suggest songs. Some systems use adaptive algorithms that adjust their behavior based on data. It is worth remembering that an algorithm is only as good as its design and its inputs. It can carry the assumptions of the people who wrote it, and results from systems of this kind should be checked with the same care as any other source, which is also true when verifying chatbot answers.
Thinking algorithmically
Writing an algorithm usually involves the steps of computational thinking: split the task into parts, notice what repeats, ignore irrelevant details and then order the steps. Then test with simple cases, including odd ones. What would the largest-number method do with an empty list? With a list of one? Testing like this finds gaps in your instructions, and fixing those gaps is what a debugging mindset is for.
You can practice anywhere: write the exact steps for tying a shoelace, then ask whether a stranger could follow them without a single question.