Professional Documents
Culture Documents
Chapter 3
Chapter 3
You probably have encountered a multitude of algorithms in your study yet. For example, an
algorithm for converting numeric representations from one form to another, detecting and
correcting errors in data, controlling time sharing in a multitasking environment or just making
a cup of tea. Algorithms can be expressed in a machine language and executed by a machine
whose CPU carries out its task by following the algorithm.
This definition means that the algorithm must have set of step which must be:
Ordered.
o Steps in an algorithm must have a well-established structure
o It doesn’t mean steps are executed in sequence
Executable – steps should yield the desired results
Unambiguous – each step must state exactly what is required
Termination – it must lead to end
G Polya defined problem-solving phases which today remain the basic principles on which
attempts to teach problem-solving skills are based
These phases are not meant to be steps to be followed one after the other when trying to
solve a problem but rather phases that will be completed sometimes during the solution
process
3.2. Pseudocode
The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms
without worrying about all the syntactic details of a particular programming language.
38
The main aim of studying pseudocode is to develop a consistent, concise notation for
representing semantic structures.
The following are examples of Semantic structures: also called Pseudocode Primitives
name = expression
Where name is the descriptive name and expression describes the value to be associated with
name. Such statements are read as “assign name the value of expression”. That is, the value
of the expression is assigned to name. For example,
Assigns the results of adding the values of Checking balance and SavingsBalance to the name,
RemainingFunds
If semantic structure – which allows the selection of one of the two possible
activities depending on the truth or falseness of some condition.
39
else (activity)
In the example above we have used the key words if, then, and else to announce the
different structures within the main structure and have used parentheses to delimit
boundaries of these structures.
We can still use a shorter syntax structure without the else part as follows,
For example
if (sales have decreased) then (lower the price by 5%)
40
In short, such statement means to check the condition and if it is true, perform the activity
and return to check the condition again. If the condition is found to be false, move to the next
structure following the while structure
Design a Pseudocode which counts all the vowels on a given page of text
Your Pseudocode must consider all the letters in a particular page and only count the vowels
in that page.
Curr_letter = 1
Last_letter = 4
Count = 0
Repeat
Read letter
If letter is vowel
Then (Count = count + 1)
Curr_letter = curr_letter + 1
41
Until curr_letter > last_letter
The trace table is a very useful tool which allows you to see the state of your algorithm with
as much details as you wish. It consists of a table in which each rows shows the state of step
in the algorithm and each column shows the value of a variable at that step. It also allows you
to check for errors.
Pseudocodes are used to describe the activities that can be used as an abstract tool in other
applications. Computer science has a variety of terms such as the procedure units, and we use
the term to announce the title by which the pseudocode unit will be known e.g.,
Procedure Greetings
Count = 3;
While (count>0) do
(print the message “hello” and
count = count – 1);
end
The task performed by a procedure will be normally required elsewhere in the pseudocode. It
will be requested by its name.
For example,
42
If two procedures were named Processloan and Rejectapplication, then we could request
their service within an if-then-else structure by the following:
If (condition) then
(execute the procedure Processloan)
else (execute the Procedure RejectApplication)
Which could results in the execution of procedure ProcessLoan if the tested condition were
true or in the execution of the RejectApplication if the condition were false.
We have been discussing problem solving from a somewhat philosophical point of view while
avoiding a direct confrontation with the question of how we should go about trying to solve a
problem. There are numerous problem solving approaches, each of which can be successful in
certain settings. But, however, there seem to be a common thread running these techniques,
which simply stated is “getting your foot in the door”. As an example let us consider the
following simple problem
Only one of these predictions was true, and this was the prediction made by the winner. In
what order did they finish the race.
After reading the problem and analyzing the data, it should not take long to realize that since
the prediction of A and D were equivalent and only one prediction was true, the prediction of
both A and D must be false. Thus, neither A and D were winners. At this point we have our foot
43
in the door, obtaining the complete solution to our problem is merely a matter of extending
our knowledge from. If A’s prediction was false, then B did not win either. The only remaining
choice is C, thus C won the race, and C’s prediction was true. Consequently, we know that A
came third. That means that the finishing order was either CBAD or CDAB. But the former is
ruled out because B’s prediction must be false. Therefore the finishing order was CDAB.
Loop Control
They control the repetitive use of an instruction or sequence of instruction. One method of
implementing such repetition is the iterative structure known as the loop, in which a collection
of instructions, called the body of the loop is executed in a repetitive fashion under the
direction of some control process.
The test for termination occurs before the loop’s body is executed.
That is,
Check the condition
Execute the body
.
.
44
.
until the condition fails.
As a general rule, the use of a loop structure produces a higher degree of flexibility than would
be obtained by merely writing the body several times.
However the statement “add a drop of sulfuric acid” three times, can be written and
controlled by the while statement as follows:
Count = 3
While (count > 0) do
(add a drop of sulfuric acid)
count = count – 1;
Repeat loop
The While loop and Repeat loop, differ in the order in which the loop control components are
executed. The Repeat loop structure is as follows
45
Unlike the While loop; Repeat loop request that the body of the loop be executed before the
test for termination is performed.
In this case, the loop’s body is always performed at least once, whereas in the While structure,
the body is never executed if the termination condition is satisfied the first time is tested.
For example,
Count = 0
Repeat (display count)
Add 1 to count
Until (count > 4)
Repeat loop execute the statement until the condition becomes true.
For loop
The For loop structure is similar to that of while statement, the different is that all the
initialization, modification and termination of the loop are incorporated into a single
statement. The For loop iterates for a fixed number of steps. For example
46
Control statement/structures
Let us now look into some of the repetitive structures that are used in describing algorithmic
process.
Consider the problem of searching a list for the occurrence of a particular target value
The algorithm should return success if the value is in the list otherwise failure
We assume names are sorted.
If we reach a name greater than the target value the search terminates
47
Select the first entry in the list as TestEntry
While (TargetValue > TestEntry and
there remain entries to be considered)
do (select the next entry in the list as TestEntry)
When the while structure terminates, one of the two conditions will be true: either the target
value has been found or the target value is not in the list.
A successful search can the detected by comparing the test entry to the target value. If they
are equal, the search has been successful. Therefore we should add the following statement
at the end of the pseudocode routine
It is also observed that the first statement in our routine, which selects the entry in the list to
play the role of test entry, is based on the assumption that the list is no empty. To be on the
safe side, we can position our routine as the else option of the following statement
This produces the procedure shown in Figure 3.1. This procedure can be used from within
other procedures by using statements such as,
Apply the procedure search to the passenger list to look for the name Darrel Baker.
(To find out if Darrel Baker is a passenger or not)
48
The algorithm represented in Figure 3.1, sequentially considers the entries in the order in
which they occur in the list. Hence it is called sequential search algorithm. It is good for short
list; however, for long list other techniques are preferred to it.
The control of a loop consists of the activities initialize, test and modify, with the presence of
each required for a successful loop control. The test activity has the obligation of causing the
termination of the looping process by watching for a condition that indicates termination
should take place. The condition is known as the termination condition.
Initialization:
(Select the first entry in the List to be TestEntry)
Modification:
(Select the next entry in List as TestEntry)
Termination:
(Targetvalue<=TestEntry) or (there are no more entries to be
considered)
The termination condition is the negation of the condition appearing in the while structure.
Thus, having executed the initialization step, repeated application of the modification step,
results in the termination condition being reached.
We should emphasize that the initialization and modification step must lead to the
appropriate termination condition. This characteristic is critical for proper loop control, and
thus one should always double-check for its presence when designing a loop structure. Failure
to make such an evaluation can lead to errors even in the simplest cases.
Here the termination condition is for the value of number to be 6. But the value of Number is
initialized at 1 and then incremented by 2 in the modification step. Thus, as loop cycles, the
values assigned to number will be 1, 3, 5, 7, and so on, but never the value 6. In turn, the loop
will never terminate.
As an additional example of iterative structure, let us consider the problem of sorting list of
names into alphabetic order. We want to sort the list “within itself”.
Let us now consider how we might sort the names on the desktop
49
Consider the following names
Fred
Alice
David
Bill
Carol
One approach to sorting this list is to note that the sub-lists consisting of only the top name,
Fred is sorted but the sub-list consisting of the top two names, Fred and Alice, is not. So we
might pick up the card containing the name Alice, slide the name Fred down into the space
where Alice was, and then place the name Alice in the hole at the top of the list, as represented
by the first row Figure 3.2. At this point our list would be
Alice
Fred
David
Bill
Carol
50
Figure 3.2.: sorting list
Now the top two names form a sorted sub-list, but the top three do not. Thus we might pick
up the third name, David, slide the name Fred, down into the hole where David was, and then
insert David in the hole left by Fred, as summarized in the second row of Figure 3.2.
The top three entries in the list are now sorted. Continuing in this fashion, we could obtaining
a list in which the top four entries are sorted by picking up the fourth name , Bill, sliding the
name, Fred and David down, then inserting Bill in the hole. Finally, we can complete the
sorting by picking up Carol, sliding, Fred and David down, and then inserting Carol in remaining
hole.
Having analyzed the process of sorting a particular list, our task now is to generalize this
process to obtain an algorithm for sorting general lists. To this end, we observe that each row
of Figure 3.2, represent the same general process:
Pick up the unsorted name,
Slide the name greater than the extracted one down
Insert the extracted name back
The algorithm has two loops, let’s start with the inner loop
51
The inner loop should be executed repeatedly. To begin the sorting process, the pivot should
be the second entry in the list and then, before each additional execution, the pivot selection
should be one more entry down the list until the last entry has been positioned.
Following this lead, we can control the required repetition with the statement
N=2
While (the value of N does not exceed the length of the List) do
(Select the Nth entry in the List as the pivot entry)
.
.
.
N = N+1
Where the “length of List” refers to the number of entries in the list and the dots indicates the
location where previous routine should be placed
The pseudocode for the algorithm is shown below in Figure 3.3. In short, the program sorts a
list by repeatedly moving an entry and inserting it into its proper place. It is because of this
repeated insertion process that the underlying algorithm is called insertion sort.
Note that the structure of Figure 3.3 is that of a loop within a loop, the outer loop being
expressed by the first while statement and the inner loop represented by the second while
statement. Each execution of the body of the outer loop results in the inner loop being
initialized and executed until its termination condition is obtained. Therefore, a single
execution of the outer loop’s body will results in several execution of the inner loop’s body
The initialization component of the outer loop’s control consist of establishing the initial value
of N with the statement
N=2
Modification component is handled by incrementing N at the end of the loop’s body with the
statement
N= N+1
The termination condition occurs when the value of N exceed the length of the List
52
The inner loop’s control:
Initialization:
Move the pivot entry from the list and thus creating a hole.
Modification:
Moving entries down into the hole, thus causing the hole to move up
Termination
Consist of the hole being immediately below a name that is not greater than
the pivot or of the hole reaching the top o the list.
Exercise 3.1
1. The Euclidean algorithm finds the greatest common divisor of two positive integers X
and Y by the following process
As long as the value of neither X nor Y is zero, continue dividing the larger of
the values by the smaller and assigning X and Y the values of the divisor and
remainder, respectively. The final value of X is the greatest common divisor.
Write a pseudocode for this algorithm
2. Design an algorithm (expressed in pseudocode) for finding all the factors of a positive
integer. For example, in the case of the integer 12, your algorithm should report
1,2,3,4,6, and 12.
3. The factorial of 0 is defined to be 1. The factorial of a positive integer is defined to be
the product of that integer times the factorial of the next smaller nonnegative integer.
We use the notation n! to express the factorial of the integer n. Thus the factorial of
3 (written 3!) is 3x (2!) = 3x(2x(1!)) = 3x(2x(1x(0!))) = 3x(2x(1x(1))) = 6. Design a
recursive algorithm that computes the factorial of any given value.
4. Design an algorithm that, given a list of names, finds the longest name in the list.
Determine what your solution does if there are several “longest” names in the list. In
particular what would your algorithm do if all the names had the same length?
5. Consider the sequential search algorithm of Figure 3.1.
a) Given the following names; [Agnes, Bobby, Edmas, Frank, Jonty, Maggy,
Onnie, Xoli and Zungu]. Use a trace table to detect the absence of Pat in
the list.
b) Modify the sequential search algorithm in Figure 3.1 to allow for lists that
are not sorted
c) Base on your answer in b above, use a trace table to detect the absence
or presence of the Emma in the following list. [John, Edga, Abby, Monto,
Elvis, Zungu, Onnie, Aggy, Emmy, and Pat]
6. Convert the following pseudocode routine to an equivalent routine using a repeat
statement.
Z=0
X=1
While (X < 6) do
(Z = Z+X
X = X+1)
53
7. Summarize the following rat’s nest routine with a single If-then-else statement.
If X > 5 then Goto 80
X=X+1
Goto 90
80: X = X+2
90: Stop
8. The following algorithm is designed to print the beginning of what is known as
Fibonacci sequence. Identify the body of the loop. The initialization of the loop
control? The modification step? The test step? What list of numbers is produced? (use
a trace table to show list of numbers)
Last = 0;
Current = 1;
While (current < 100) do
(print the value assigned to Current;
Temp = last
Last = Current
Current = Last + Temp)
Recursive structures provide an alternative to the loop paradigm for repetitive structures.
Whereas a loop involves repeating a set of instructions in a manner in which the set is
completed and then repeated, recursion involves repeating the set of instruction as a subtask
of itself. As a way of introducing recursion, we consider the binary search algorithm, which
applies divide-and-conquer methodology to the search process.
Let us again tackle the problem of searching to see whether a particular entry is in a sorted
list, but this time we consider the procedure we follow when searching a dictionary. In this
case we do not perform a sequential entry-by-entry or page-by-page procedure. Rather, we
begin by opening the dictionary to a page in the area where we believe the target entry is
located. If we are lucky, we will find the target entry there; otherwise, we must continue
searching. But at this point we will have narrowed our search considerably.
In binary search,
We assumes the list is sorted
The search always begins with the middle entry
On even entries, the first entry in the second half of the list is regarded as the
middle entry
If the middle entry is not the target, the search is restricted either to first or last
half of the list, and continues searching.
The approach to the searching processes is summarized in Figure 3.4, where we consider the
task of searching the list on the left of the figure for the entry John. We first consider the
54
middle entry Harry. Since our target belongs after this entry, the search continues by
considering the lower half of the original list. The middle of this sub-list is found to be Larry.
Since our target should precede Larry, we turn our attention to the first half of the current
sub-list. When we interrogate the middle of that secondary sub-list, we find our target John
and declare the search success. In short, our strategy is to successively divide the list in
question into smaller segments until the target is found or the search is narrowed to an empty
segment. If the target value is not in the original list, our approach to searching list will proceed
by dividing the list into smaller segments until the segment under consideration is empty. At
this point the algorithm should recognize that the search is a failure.
The binary search algorithm is similar to the sequential search in that each algorithm requests
the execution of a repetitive process. However, the sequential search involves a circular form
of repetition. The binary search executes each stage of the repetition as a subtask of the
previous stage. This technique is known as recursion.
55
Figure 3.5: binary search algorithm expressed in pseudocode
Exercise 3.2
1. Suppose we find that a machine programmed with insertion sort algorithm requires
an average on one second to sort a list a 100 names. How long do you estimate it takes
to sort a list 1000 names? How about 10,1000 names
2. Design an algorithm that, given two strings of characters, tests whether the first string
appears as a substring somewhere in the second.
3. The following is an additional base ten notation. Each letter represents a different
digit. What does each letter represents? How did you get your foot in the door?
XYZ
+ YWY
---------
ZYZW
Solutions
Exercise 3.1
1.
X = the larger input
Y = the smaller input
While (Y or X not Zero) do
(Remainder =Remainder after dividing X by Y)
56
X=Y
Y = Remainder;
GCD = X
3. procedure Factorial (Value)
if (Value is 0)
then (Return 1 as the answer)
else (Apply Factorial to (Value - 1),
multiply the result by Value, and
X = the value of this product),
return the number assigned to X as the answer)
6.
Z=0
X=1
reapeat (Z = Z+X
X = X+1)
Until (X=6)
57