We have seen that before a computer can perform a task, it must be given an algorithm telling it precisely what to do; consequently, the study of algorithms is the cornerstone of computer science. In this chapter we introduce many of the fundamental concepts of this study, including the issues of algorithm discovery and representation as well as the major control concepts of iteration and recursion. In so doing we also present a few well-known algorithms for searching and sorting. We begin by reviewing the concept of an algorithm.
The Concept of an Algorithm
In the introductory chapter we informally defined an algorithm as a set of steps that define how a task is performed. In this section we look more closely at this fundamental concept.
An Informal Review
We have encountered a multitude of algorithms in our study. We have found algorithms for converting numeric representations from one form to another, detecting and correcting errors in data, compressing and decompressing data files, controlling multiprogramming in a multitasking environment, and many more. Moreover, we have seen that the machine cycle that is followed by a CPU is nothing more than the simple algorithm. As long as the halt instruction has not been executed
continue to execute the following steps:
a. Fetch an instruction.
b. Decode the instruction.
c. Execute the instruction.
As demonstrated by the algorithm describing a magic trick, algorithms are not restricted to technical activities. Indeed, they underlie even such mundane activities as shelling peas:
Obtain a basket of unshelled peas and an empty bowl.
As long as there are unshelled peas in the basket continue to execute the following steps:
a. Take a pea from the basket.
b. Break open the pea pod.
c. Dump the peas from the pod into the bowl.
d. Discard the pod.
In fact, many researchers believe that every activity of the human mind, including imagination, creativity, and decision making, is actually the result of algorithm execution—a conjecture we will revisit in our study of artificial intelligence.
But before we proceed further, let us consider the formal definition of an algorithm.
The Formal Definition of an Algorithm
Informal, loosely defined concepts are acceptable and common in everyday life, but a science must be based on well-defined terminology. Consider, then, the formal definition of an algorithm.
Note that the definition requires that the set of steps in an algorithm be ordered. This means that the steps in an algorithm must have a well-established structure in terms of the order of their execution. This does not mean, however, that the steps must be executed in a sequence consisting of a first step, followed by a second, and so on. Some algorithms, known as parallel algorithms, contain more than one sequence of steps, each designed to be executed by different processors in a multiprocessor machine. In such cases the overall algorithm does not possess a single thread of steps that conforms to the first-step, second-step scenario. Instead, the algorithm’s structure is that of multiple threads that
branch and reconnect as different processors perform different parts of the overall task. (We will revisit this concept. Other examples include algorithms executed by circuits such as the flip-flop, in which each gate performs a single step of the overall algorithm. Here the steps are ordered by cause and effect, as the action of each gate propagates throughout the circuit. Next, consider the requirement that an algorithm must consist of executable steps. To appreciate this condition, consider the instruction
Make a list of all the positive integers which would be impossible to perform because there are infinitely many positive integers. Thus any set of instructions involving this instruction would not be
an algorithm. Computer scientists use the term effective to capture the concept of being executable. That is, to say that a step is effective means that it is doable. Another requirement imposed by the definition is that the steps in an algorithm be unambiguous. This means that during execution of an
algorithm, the information in the state of the process must be sufficient to determine uniquely and completely the actions required by each step. In other words, the execution of each step in an algorithm does not require creative skills.
Rather, it requires only the ability to follow directions. An algorithm define a terminating
process, which means that the execution of an algorithm must lead to an end. The origin of this requirement is in theoretical computer science, where the goal is to answer such questions as “What are the ultimate limitations of algorithms and machines?” Here computer science seeks to distinguish between problems whose answers can be obtained algorithmically and problems whose
answers lie beyond the capabilities of algorithmic systems. In this context, a line is drawn between processes that culminate with an answer and those that merely proceed forever without producing a result.
There are, however, meaningful applications for nonterminating processes, including monitoring the vital signs of a hospital patient and maintaining an aircraft’s altitude in flight. Some would argue that these applications involve merely the repetition of algorithms, each of which reaches an end and then automatically repeats. Others would counter that such arguments are simply attempts to cling to an overly restrictive formal definition. In any case, the result is that the term algorithm is often used in applied, or informal settings in reference to sets of steps that do not necessarily define terminating processes. An example is the long-division “algorithm” that does not define a terminating process for dividing 1 by 3. Technically, such instances represent misuses of the term.


