Computer Science · Ch 4 — Introduction to Problem Solving
Developing an Algorithm
Developing an Algorithm
Before a single line of program code is written, a solution to the problem must be devised. That solution, expressed in natural language as a set of steps, is called an algorithm.
Algorithm — a set of exact steps which, when followed, solve the problem or accomplish the required task.
The recipe analogy
A helpful way to picture an algorithm is as a very well-written recipe for a dish: it lays out clearly defined steps which, if followed faithfully, are guaranteed to end with the dish prepared. In the same way, an algorithm lays out exact steps which, if followed, produce the solution to the problem.
Refining the algorithm
An algorithm rarely emerges perfect on the first attempt. The practical approach is:
- start with a tentative plan of the solution,
- keep refining it, pass after pass,
- stop only when the algorithm captures every aspect of the desired solution.
It is also normal for a single problem to admit more than one algorithm. When that happens, our job is to compare the candidates and select the most suitable solution (how algorithms are compared is taken up in section 4.7; the algorithm itself is discussed in detail in section 4.3).