Chapter 08
Chapter 08
Algorithms
q Define an algorithm and relate it to problem solving.
q Define three construct and describe their use in algorithms.
q Describe pseudocode and how they are used in algorithms.
q Describe UML diagrams and how they are used in algorithms.
q List basic algorithms and their applications.
q Describe the concept of sorting and understand the mechanisms
behind three primitive sorting algorithms.
q Describe the concept of searching and understand the
mechanisms behind two common searching algorithms.
q Define subalgorithms and their relations to algorithms.
q Distinguish between iterative and recursive algorithms
8.1.1 Informal definition
An informal definition of an algorithm is:
Algorithm: a step-by-step method for solving a problem
or doing a task.
Processing
The algorithm uses the five steps to find the largest integer:
Output
Because there are no more integers to be processed, the
algorithm outputs the value of Largest, which is 13.
8.1.3 Defining actions
Figure 8.2 does not show what should be done in each step.
We can modify the figure to show more details.
Figure 8.3 Defining actions in FindLargest algorithm
8.1.4 Refinement
This algorithm needs refinement to be acceptable to the
programming community. There are two problems. First, the
action in the first step is different than those for the other
steps. Second, the wording is not the same in steps 2 to 5.
We can easily redefine the algorithm to remove these two
inconveniences by changing the wording in steps 2 to 5 to
“If the current integer is greater than Largest, set Largest to
the current integer.” The reason that the first step is different
than the other steps is because Largest is not initialized. If
we initialize Largest to −∞ (minus infinity), then the first
step can be the same as the other steps, so we add a new
step, calling it step 0 to show that it should be done before
processing any integers.
Figure 8.4 FindLargest refined
8.1.5 Generalization
Is it possible to generalize the algorithm? We want to find
the largest of n positive integers, where n can be 1000,
1,000,000, or more. Of course, we can follow Figure 8.4 and
repeat each step. But if we change the algorithm to a
program, then we need to actually type the actions for n
steps!
There is a better way to do this. We can tell the
computer to repeat the steps n times. We now include this
feature in our pictorial algorithm (Figure 8.5).
Figure 8.5 Generalization of FindLargest
8.14
Figure 8.6 Three constructs
8.2.1 Sequence
The first construct is called the sequence. An algorithm, and
eventually a program, is a sequence of instructions, which
can be a simple instruction or either of the other two
constructs.
8.2.2 Decision
Some problems cannot be solved with only a sequence of
simple instructions. Sometimes we need to test a condition.
If the result of testing is true, we follow a sequence of
instructions: if it is false, we follow a different sequence of
instructions. This is called the decision (selection) construct.
8.16
8.2.3 Repetition
In some problems, the same sequence of instructions must be
repeated. We handle this with the repetition or loop
construct. Finding the largest integer among a set of integers
can use a construct of this kind.
8.17
8.18
8.3.1 UML
Unified Modeling Language (UML) is a pictorial
representation of an algorithm. It hides all the details of an
algorithm in an attempt to give the “big picture” and to show
how the algorithm flows from beginning to end. UML is
covered in detail in Appendix B. Here we show only how the
three constructs are represented using UML (Figure 8.7).
8.19
Figure 8.7 UML for three constructs
8.3.2 Pseudocode
Pseudocode is an English-language-like representation of an
algorithm. There is no standard for pseudocode—some
people use a lot of detail, others use less. Some use a code
that is close to English, while others use a syntax like the
Pascal programming language. Pseudocode is covered in
detail in Appendix C. Here we show only how the three
constructs can be represented by pseudocode (Figure 8.8).
8.21
Figure 8.8 Pseudocode for three constructs
Example 8.1
Solution
Example 8.2
Solution
Example 8.3 8.3
Solution
Example 8.4
Solution
Example 8.5
Solution
Algorithm: An ordered set of unambiguous steps that
produces a result and terminates in a finite time.
8.28
8.4.1 Well-Defined
An algorithm must be a well-defined, ordered set of
instructions.
8.4.2 Unambiguous steps
Each step in an algorithm must be clearly and
unambiguously defined. If one step is to add two integers,
we must define both “integers” as well as the “add”
operation: we cannot for example use the same symbol to
mean addition in one place and multiplication somewhere
else.
8.4.3 Produce a result
An algorithm must produce a result, otherwise it is useless.
The result can be data returned to the calling algorithm, or
some other effect (for example, printing).
8.4.3 Terminate in a finite time
An algorithm must terminate (halt). If it does not (that is, it
has an infinite loop), we have not created an algorithm. In
Chapter 17 we will discuss solvable and unsolvable
problems, and we will see that a solvable problem has a
solution in the form of an algorithm that terminates.
8.30
8.5.1 Summation
One commonly used algorithm in computer science is
summation. We can add two or three integers very easily,
but how can we add many integers? The solution is simple:
we use the add operator in a loop (Figure 8.9).