Definition
An algorithm is a finite, ordered sequence of well-defined instructions that solves a problem or achieves a goal. Every time a computer carries out a task — sorting a list, finding a route, compressing a file — it follows an algorithm.
Algorithms are not exclusive to computers. Any precise, step-by-step procedure qualifies.
Real-World Analogy: A Recipe
A cooking recipe is an algorithm:
- Preheat oven to 180°C.
- Mix flour, sugar, and butter in a bowl.
- Add eggs and stir until smooth.
- Pour mixture into a baking tin.
- Bake for 30 minutes.
- Remove from oven and allow to cool.
Like a computer algorithm, a recipe has a defined start, a series of unambiguous steps, and a defined end (a finished cake).
Key Properties of an Algorithm
A valid algorithm must satisfy these properties:
| Property | Meaning |
|---|---|
| Finite | It must eventually stop — it cannot run forever |
| Definite | Each step is precisely and unambiguously defined |
| Input | It accepts zero or more inputs |
| Output | It produces at least one result |
| Effective | Every step can actually be carried out |
An algorithm that never terminates (an infinite loop with no exit condition) is not a valid algorithm, even if every individual step is correct.
A Simple Pseudocode Example
Here is an algorithm that finds the largest number in a list:
DEFINE findMax(numbers):
max = numbers[0]
FOR each number IN numbers:
IF number > max:
max = number
RETURN max
Step by step:
- Start by assuming the first number is the largest.
- Go through every number in the list.
- If the current number is bigger than the current maximum, update the maximum.
- After checking every number, return the maximum found.
Algorithms vs Programs
An algorithm is an idea — a logical sequence of steps. A program is a specific implementation of that idea in a programming language. The same algorithm can be written in Python, Java, or any other language; the underlying logic remains the same.
Before writing any code, it is good practice to design the algorithm first — in plain language or pseudocode. This separates the problem-solving thinking from the syntax details of a specific language.
Practice Problems
Name the five key properties that every algorithm must have.
Show answer
What is the difference between an algorithm and a program?