Foundations & Complexity

algorithm

An algorithm is a recipe: a finite list of clear, unambiguous steps that turns some input into the answer you want. A cooking recipe takes ingredients and gives you a meal; a route from your map app takes two addresses and gives you turn-by-turn directions; long-division as you learned it in school takes two numbers and gives you a quotient. In each case the steps are precise enough that anyone — or any computer — following them faithfully gets the same result. That faithfulness is the whole point: an algorithm is the idea, written down so plainly that it no longer needs a clever human to interpret it.

To count as an algorithm, a procedure usually must be definite (each step is exact, with no 'do something sensible here'), finite (it stops after a bounded number of steps rather than running forever), and effective (each step is something you could actually carry out by hand). It also has well-defined inputs and a well-defined output. Notice that an algorithm is separate from any one program: the same merge sort algorithm can be written in C++, Python, or on paper. The code is one expression of the algorithm; the algorithm is the strategy behind the code.

Why do we fuss over this distinction? Because most interesting problems have many algorithms that all produce the correct answer, yet differ enormously in how much time and memory they need as the input grows. Choosing well — and proving your choice is correct and efficient — is the heart of this whole subject. We analyze an algorithm's running time and space with asymptotic tools (see Big-O notation) precisely so we can compare two correct recipes and pick the one that will still finish when the input gets large.

Named for the 9th-century mathematician al-Khwarizmi. An algorithm is the strategy; a program is one written-out version of it in a particular language.

Also called
proceduremethod算法演算法