An algorithm as a precise method for a defined problem
An algorithm is a finite sequence of precise steps that solves a defined problem. It takes some input, follows the steps, and produces an output. A recipe is a familiar comparison, though a computer algorithm must leave no step to personal judgement.
Textbooks usually list the required properties. The steps must be unambiguous. The procedure must finish after a finite number of steps. It has defined inputs and at least one output. Each step must be simple enough to carry out.
Flowcharts and pseudocode as ways to write an algorithm
A flowchart draws an algorithm as shapes joined by arrows. An oval marks the start and the end. A rectangle marks a process step, a parallelogram marks input or output, and a diamond marks a decision with yes and no branches. The arrows show the order of the steps.
Pseudocode is the text alternative. It uses plain statements such as if and repeat, without the strict syntax of a programming language. The same algorithm can be given as a flowchart, as pseudocode, or as code in any language.
Worked example: finding the largest number in a list
Take the list 4, 9, 2, 7. Step 1: set max to the first number. Step 2: for each remaining number, if it is greater than max, set max to that number. Step 3: when no numbers remain, output max. In a flowchart, step 2 is a loop containing one decision diamond.
Trace it. Start with max = 4. Compare 9: 9 is greater than 4, so max = 9. Compare 2: 2 is not greater than 9, so max stays 9. Compare 7: 7 is not greater than 9, so max stays 9. The output is 9.
The algorithm made 3 comparisons for 4 numbers. For a list of n numbers it makes n minus 1 comparisons, so 4 minus 1 gives 3.
Common misconceptions
An algorithm is not the same as a program. A program is an implementation of one or more algorithms in a specific language, and one algorithm can be implemented in many languages. Correct algorithms can also differ greatly in speed and memory use, which is why efficiency is measured by how running time grows as the input grows.
Algorithms do not need a computer. The schoolbook method for long division is an algorithm, because its steps are exact and it finishes.
Where algorithms show up
Algorithms appear in sorting and searching, route finding in maps, data compression, encryption, and database query processing. Operating systems use scheduling algorithms to decide which task the CPU runs next. Any fixed procedure with exact steps and a result can be described this way.
Key terms
- Algorithm
- A finite sequence of unambiguous steps that turns an input into an output and solves a defined problem.
- Flowchart
- A diagram that shows the steps of an algorithm as shapes connected by arrows.
- Pseudocode
- A plain-language description of an algorithm that does not follow the strict syntax of a programming language.
- Input and output
- The data an algorithm receives at the start and the result it delivers at the end.
- Decision
- A step that tests a condition and chooses between branches, drawn as a diamond in a flowchart.
- Loop
- A set of steps repeated until a condition is met.
- Efficiency
- A measure of how much time or memory an algorithm uses as the size of its input grows.
