and algorithm
1
. Informally, you can think of an algorithm as a strategy for solving a
problem. To appreciate how computer scientists use the term, however, it is necessary
to formalize that intuitive understanding and tighten up the definition.
To be an algorithm, a solution technique must fulfill three basic requirements.
First of all, an algorithm must be presented in a clear, unambiguous form so that it is
possible to understand what steps are involved. Second, the steps within an algorithm
must be effective, in the sense that it is possible to carry them out in practice. A
technique, for example, that includes the operation “multiply r by the exact value of
п” is not effective, since it is not possible to compute the exact value ofп.Third, an
algorithm must not run on forever but must deliver its answer in a finite amount of time.
In summary, an algorithm must be
1. Clearly and unambiguously defined
2. Effective, in the sense that its steps are executable.
3. Finite, in the sense that it terminates after a bounded number of steps.
These properties will turn out to be more important later on when you begin to
work with complex algorithms. For the moment, it is sufficient to think of algorithms
as abstract solution strategies—strategies that will eventually become the core of the
programs you write.
As you will soon discover, algorithms—like the problems they are intended to
solve — vary significantly in complexity. Some problems are so simple that an
appropriate algorithm springs immediately to mind, and you can write the programs to
solve such problems without too much trouble. As the problems become more complex,
however, the algorithms needed to solve them begin to require more thought. In most
cases, several different algorithms are available to solve a particular problem, and you
need to consider a variety of potential solution techniques before writing the final
program. You will have a chance to revisit this topic in Chapter 6, which addresses how
to decide which algorithm is best for a given problem.
1-5 Programming languages and compilation
Solving a problem by computer consists of two conceptually distinct steps. First,
you need to develop an algorithm, or choose an existing one, that solves the problem.
This part of the process is called algorithmic design. The second step is to express that
algorithm as a computer program in a program in a programming language. This
process is called coding.
As you begin to learn about programming, the process of coding—translating
your algorithm into a functioning C program—will seem to be the more difficult phase
of the process. As a new programmer, you will, after all, be starting with simple
problems just as you would when learning any new skill. Simple problems tend to have
simple solutions, and the algorithmic design phase will not seem particularly
challenging. Because the language and its rules are entirely new and unfamiliar,
however, coding may at times seem difficult and arbitrary. I hope it is reassuring to say
that coding will rapidly become easier as you learn more about the programming
process. At the same time, however, algorithmic design will get harder as the problems
you are asked to solve increase in complexity.
When new algorithms are introduced in this text, they will usually be expressed
initially in English. Although it is offer less precise than one would like, English is a
reasonable language in which to express solution strategies as long as the
communication is entirely between people who speak English. Obviously, if you
wanted to resent your algorithm to someone who spoke only Russian, English would
1
The word algorithm comes to use from the name of the ninth-century Arabic mathematician
Abu Ja’far Mohammed ibn Mûsâ al –Knowârizmî, who wrote a treatise on mathematics entitled
Kitab al jabr w’almuqabala (which itself gave rise to the English word algebra).