An algorithm is a set of ordered, clear steps that solves a problem or completes a task and then ends. A recipe, directions to a friend's house, and a setup guide are all algorithms. Programming means writing those steps in a language a computer understands. So the first skill in programming is to describe a task step by step, without leaving anything unclear.
What is an example of an algorithm in everyday life?
Making tea is one:
1. Put water in a kettle and turn on the heat.
2. Put a tea bag in a cup.
3. When the water boils, pour it into the cup.
4. Wait 4 minutes.
5. Remove the tea bag.
It looks simple, but if we explained it to a computer, the questions would not stop. How much water? How do you know it is boiling? A person fills these gaps with experience. A computer does not. It does only what you write, so every step must be fully defined.
A good algorithm has these properties:
| Property | Meaning |
|---|---|
| Clear | Each step has one meaning |
| Ordered | The order of steps is fixed |
| Finite | It ends at some point |
| Input | It works on some information |
| Output | It produces a result |
"Wait a bit" is not clear. "Wait 4 minutes" is clear. "Wait until the water boils" is clear only if you have a way to check that the water is boiling.
How do you write a simple algorithm?
Take a problem: the scores in a class are 72, 85, 58, 90, and 64. What is the highest score?
With five scores, you look and say "90". With 5,000 scores, looking is not enough. You need a method that works for any number of scores:
1. Remember the first score as "the highest so far".
2. Look at the next score.
3. If it is higher than the one you remember, replace the one you remember.
4. Repeat steps 2 and 3 until no scores are left.
5. The score you remember is the highest.
Let us trace it by hand with the five scores:
| Score you look at | Higher than remembered? | Highest so far |
|---|---|---|
| 72 (first score) | 72 | |
| 85 | Yes | 85 |
| 58 | No | 85 |
| 90 | Yes | 90 |
| 64 | No | 90 |
After four comparisons, the result is 90. Tracing an algorithm line by line with a small example is the simplest way to check that it works.
What does an algorithm look like in Python?
Here is the same algorithm in a few lines of Python:
scores = [72, 85, 58, 90, 64] highest = scores[0] for score in scores[1:]: if score > highest: highest = score print(highest)
90
Each line matches a step of the algorithm:
| Algorithm step | Python |
|---|---|
| Remember the first score | highest = scores[0] |
| Look at each next score until none are left | for score in scores[1:]: |
| Replace it if the new score is higher | if score > highest: and highest = score |
| Report the result | print(highest) |
Python also has a built-in max(scores) function. It does the same work at its core: it goes through the values and keeps the highest one so far.
What is the difference between an algorithm and a program?
An algorithm is the idea: the steps, in any form, such as a list in plain language or a drawing. A program is one implementation of that idea, written in a programming language such as Python. The same algorithm can become a program in Python, in another language, or a set of steps you follow by hand.
You can also draw an algorithm as a flowchart:
- Oval: start and end
- Rectangle: an action (
highest = score) - Parallelogram: input or output (print
highest) - Diamond: a decision (is
score > highest?) - Arrows: the order of the steps
Drawing the algorithm or writing it as a list before you code helps you find logic errors before you write a single line.
Where else do you use algorithmic thinking?
Algorithmic thinking is not only for programming:
- Planning an exam week: which subject first, how much time for each
- Looking for a lost item: the most likely places first, then the rest in order
- Changing a recipe to make double the portions
In each case you break a problem into parts, put the steps in order, and make each step clear.
Common mistakes
- Skipping steps. "Everyone knows this", so you do not write it. A computer does not know it.
- Unclear wording. Words like "a little", "enough", and "at a good time".
- A loop that never ends. You did not say when the repeat should stop.
- Forgetting edge cases. If the list of scores is empty, the step "remember the first score" is impossible. The code above would stop with
IndexError: list index out of rangeon thescores[0]line. The algorithm needs a step such as "if the list is empty, stop".
What to read next
- Plan before you code in the [algorithm plan first](/learn/python/algorithm-plan-first) lesson.
- Write your first program in [first program](/learn/python/first-program), part of the [Python course](/courses/python).
- Learn how to handle mistakes in [learning from mistakes when you learn to code](/blog/learning-from-mistakes-coding).
- #algorithm
- #beginners
- #programming
- #problem solving