What is an algorithm?
An algorithm is a finite, ordered set of clear steps that solves a problem or performs a task. A recipe, directions to a friend's house and the steps to withdraw money from an ATM are all algorithms.
A program is an algorithm written in a language a computer understands. Good programmers design the algorithm first and code afterwards.
Algorithm, program and code
These three words are related but they are not the same:
| Term | What it is | Example |
|---|---|---|
| Algorithm | The plan: the steps, written in any form a person can follow | "Add the three marks and divide by 3" |
| Program | The algorithm written in a programming language | A Java class that reads three marks and prints the average |
| Code | The actual text of the program | double avg = (a + b + c) / 3.0; |
One algorithm can be turned into many programs: the same steps can be written in Java, Python or C++. That is why programmers design the algorithm first. Once the logic is right, translating it into a language is the easy part.
Real-life example: a cake recipe is an algorithm. The ingredients are the input, the mixing and baking steps are the process, and the cake is the output. If the recipe says "bake until ready" without a time or a test, a beginner cannot follow it. That step is not definite.
Properties of a good algorithm
| Property | Meaning |
|---|---|
| Input | Zero or more values are supplied |
| Output | At least one result is produced |
| Definiteness | Every step is clear and unambiguous |
| Finiteness | It ends after a limited number of steps |
| Effectiveness | Every step is simple enough to be carried out |
| Correctness | It gives the right answer for all valid inputs |
Try it: write the steps for making a cup of tea, then ask a friend to follow them exactly as written. Every step they cannot follow, or follow wrongly, is a missing or ambiguous step: the property of definiteness at work.
Each property explained
A list of steps is only a good algorithm when it has all six properties. Here is what each one means in practice:
- Input: the data the algorithm works on. "Find the average of three marks" needs three marks as input. Some algorithms need no input at all, for example "print the numbers 1 to 10".
- Output: an algorithm that produces nothing is useless. Every algorithm must give at least one result: a value shown on screen, a file saved or a decision made.
- Definiteness: each step must have exactly one meaning. "Add a few numbers" is not definite (how many? which ones?). "Add a, b and c" is.
- Finiteness: the algorithm must stop. A loop with no way to end ("keep adding 1 to x") never finishes, so it never gives an answer.
- Effectiveness: every step must be something that can really be done, with the tools available. "Find the largest prime number" is not effective, because there is no largest prime.
- Correctness: it must give the right answer for every valid input, not only for the example you tried. An average algorithm that divides by 2 instead of 3 runs perfectly but is not correct.
A poor algorithm and a better one
Problem: work out the price to pay for a shop item after a discount.
| Poor version | Better version |
|---|---|
| 1. Get the price. | 1. Start. |
| 2. Take off the discount. | 2. Read price and discountPercent. |
| 3. Show it. | 3. Set discount = price × discountPercent / 100. |
4. Set finalPrice = price − discount. | |
5. Display finalPrice. | |
| 6. Stop. |
The poor version is not definite: "take off the discount" does not say how the discount is worked out, and the discount is never read as input. The better version names every value, gives the formula and has a clear start and end.
Ways of writing an algorithm
The same algorithm can be written down in several forms. You will use all of them in this unit:
- Plain language (numbered steps): quick and easy, but can be vague. Used in this topic.
- Flowchart: a diagram of standard symbols joined by arrows. Easy to see the flow at a glance. See 1.2 Flowcharts.
- Pseudocode: structured, English-like statements that look close to real code. See 1.3 Pseudocode.
- Program code: the final form that a computer can run, for example Java.
Remember
- An algorithm is a plan; a program is that plan written in a programming language.
- A good algorithm has input, output, definiteness, finiteness, effectiveness and correctness.
- Always design and check the algorithm before you start coding.