Algorithm
An algorithm is a sequence of rigorous steps which can be followed to solve a particular type of problem. To fully describe an algorithm, you must specify:
- The algorithm's name
- The input to the problem
- The output to the problem
- The steps taken to reach the output from the input
Algorithms are often written in programming languages, which have strict rules on syntax to ensure that a computer can compile it. To avoid syntax and focus only on the actual meaning of the code, an in-between language is used: pseudocode. Here's an example of an algorithm (the Euclidean algorithm) written in pseudocode:
algorithm euclid is
input: two integers, a and b, where a ≤ b
output: their greatest common divisor (gcd)
r ← b mod a
while r ≠ 0
b ← a
a ← r
r ← b mod a
return a
More advanced algorithms perform work beyond the scope of a simple calculation, using programming constructs such as conditionals to divert code, loops for iteration, and recursion to divide problems into easier sub-problems.
The output of an algorithm is specified by a return command.