Unit content
Algorithms and programs
An algorithm is a finite, well-defined procedure for transforming inputs into outputs or for carrying out a task. A program is an algorithm or collection of algorithms expressed in a form that a computer can execute.
Consider the task of finding the largest value in a nonempty sequence of numbers. One algorithm is:
- Treat the first value as the largest seen so far.
- Inspect each remaining value in turn.
- Whenever a value is larger than the largest seen so far, make it the new largest.
- After every value has been inspected, return the largest one found.
The procedure describes what must happen without depending on a particular programming language.
Inputs, intermediate state and outputs
An algorithm begins with some available information, performs a sequence of steps and produces a result or effect. During those steps it may remember intermediate results, make choices or repeat an operation.
Correctness
An algorithm is correct when it produces the required result for every input covered by its specification.
Trying examples can reveal mistakes, but correctness is a claim about all allowed inputs, not only the cases already tested.
Termination
A procedure intended to finish must eventually reach an end. A set of instructions that can continue repeating forever does not terminate for that execution.
Programs and languages
Programming languages provide precise ways to express algorithms so that software tools and computers can process them. Later programming concepts give formal notation for values, choices, repetition, functions and decomposition; the underlying idea of an algorithm does not depend on any one of those notations.