Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Chapter 3: Introduction Algorithms

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.

An algorithm is formally defined as an ordered set of unambiguous, executable steps, defining


a termination process.

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

3.1. Problem solving

G Polya defined problem-solving phases which today remain the basic principles on which
attempts to teach problem-solving skills are based

Phase 1: understand the problem


Phase 2: Get an idea of how an algorithm procedure might solve the problem
Phase 3: formulate the algorithm and represent it as a program
Phase 4: Evaluate the problem for accuracy and for its potential as a tool for solving
other problems.

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

Pseudocode is a generic way of describing an algorithm without use of any specific


programming language syntax.
It cannot be executed on a real computer, but it models and resembles real programming
written at roughly the same level of details.

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

 Assignment semantic structure – which is a structure that assigns values to


descriptive names. The assignment statement / structure establish associations
between names and values.

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,

RemainingFunds = CheckingBalance + savingsBalance

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.

Consider the following examples,


 If the gross domestic product has increased, buy common
stock; otherwise, sell common stock
 Buy common stock if the gross domestic product has
increased and sell it otherwise
 Buy or sell common stock depending on whether the gross
domestic product has increased or decreased, respectively.
Each of these statements could be rewritten to conform to the structure
If (condition) then
(activity)

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.

As another example consider this:


Depending on whether or not the year is a leap year, divide the total by
366 or 365 respectively,

This may be written as follows:


if (year is leap year)
then (daily-total = total divided by 366)
else (daily-total = total divided by 365)

We can still use a shorter syntax structure without the else part as follows,

if (condition) then (activity)

For example
if (sales have decreased) then (lower the price by 5%)

 While semantic structure – which allows the program to keep or continue


executing a statement or sequence of statements as some condition remains true.

For example, the while structure takes this form

As long as there are tickets to sell, continue selling tickets


And
While there are tickets to sell, keep selling tickets

For such cases we adopt this uniform pattern for pseudocode

While (condition) do (activity)

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

The above statements are thus reduced as follows

While (tickets remains to be sold) do (sell tickets)

 Repeat structure – which allows the program to keep or continue executing a


statement or sequence of statements as some condition remains false.

Repeat (activity) until (condition)


E,g. Continue selling the tickets as long as there are tickets to sell

Now let’s see if we can use all these structures,

For example, suppose you are requested to:

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.

Firstly you identify the variables to be used and initialize them.


Secondly, read and identify the vowels. After identifying a vowel you will need to repeat the
say process to look for other vowels. Once a vowel has been found the count will be increased
by 1.

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

Trace Table for the above pseudocode

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.

Curr_Letter Last_letter Count Letter Is letter Vowel? Curr_letter>Last_letter


1 4 0 P No False
2 4 1 A Yes False
3 4 1 G No False
4 4 2 E Yes True
5

Procedures and functions

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 trace table for the Procedure Greetings is as follows:

Count Count>0 Output


3 yes hello
2 yes hello
1 yes hello
0 no

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.

Procedures should be designed to be as generic as possible. A procedure for sorting lists of


names should be designed to sort any list, not a particular one. It should be written in a way
that the list to be sorted is not specified in the procedure itself. Instead, the list should be
referred to by a generic name within the procedure’s representation.
For example,
The procedure sort which is designed to sort any list of names, would begin with the statement
Procedure Sort (List)
Later in the representation where a reference to the list being sorted is required, the generic
name List would be used. In turn, when the services of Sort are required, we will identify which
list is to be substituted for List in the procedure Sort. Thus, we will write something like this:
Apply the Procedure Sort to the organization’s membership list
And
Apply the procedure Sort to the wedding list
Depending on the need

Getting a foot in the door (a mind teaser)

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

Before A, B, C, and D ran a race they made the following predictions


A predicted that B would win
B predicted that D would be last
C predicted that A would be third
D predicted that A’s prediction would be correct.

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.

3.3. Iterative structures

Firstly, let’s look at the Loop controls.

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 While loop

The while loop takes on this form,


While (condition) do (body)
And its structure is represented as follows

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.

For example, the statement


“Add a drop of sulfuric acid” three times
is equivalent to the sequence
Add a drop of sulfuric acid
Add a drop of sulfuric acid
Add a drop of sulfuric acid
But we cannot produce a similar sequence that is equivalent to the loop described by:
While (the pH level is greater than 4) do
(add a drop of sulfuric acid)
Because we do not know in advance how many drops of acid will be required.

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;

Three basic steps of a Loop


 Initialization step – establishes an initial state that will be modified toward the
termination condition
 Modification step – changes the state in such a way that it moves towards the
termination
 Test / termination step – compare the current state to the termination condition
and terminate the repetition if equal.

 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.

Repeat (activity) until (condition)

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

For (counter = 1; counter <= 10; counter++)


End

46
Control statement/structures

A control statement is an imperative statement that alters the execution sequence of a


program

 Goto statement, it is the simplest control statement. It provides a means of


directing the execution sequence to another location that has been labeled by a
name or number. The problem with this statement is that it allows the
programmer to write complicated statements.
 If-then-else statement, which allows choice between two options
 Case statement, which allows selection between many options
 While statement, which allows the body to be executed if the condition is true

Let us now look into some of the repetitive structures that are used in describing algorithmic
process.

3.3.1 The sequential search algorithm

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

The process can be represented by the following pseudocode

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

if (TargetValue = TestEntry) then


(declare the search success)
else (declare the search a failure)

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

if (list empty) then


(declare search a failure)
else (…)

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)

Figure 3.1: sequential search algorithm expressed in Pseudocode

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.

From the Sequential search algorithm

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.

A typical example is found in the statements


Number =1
While (number <>6) do
(number = number + 2)

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.

3.3.2 The insertion sort algorithm

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

We will call the extracted name to be the pivot entry

Move the pivot entry to a temporary location leaving a hole in List


While (there is a name above the hole and that name is greater than the pivot) do
(move the name above the hole down into the hole leaving a hole above the
name)
move the pivot entry into the hole in List

Now the outer 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.

Figure 3.3: insertion sort algorithm expressed in pseudocode.

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)

3.8. Recursive Structures

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.

3.8.1. The binary Search algorithm

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.

Figure 3.4: searching a list for entry John

The binary search algorithm is represented by Figure 3.5 below.

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)

4 if (the list is not empty)


then(Longest = the first entry in the list;
P = 2;
while (P <= length of list) do
(if (the Pth entry in the list is longer than Longest)
then (Longest = the Pth entry);
P = P + 1)
If there are more than one longest entry, this algorithm reports the first one.

6.
Z=0
X=1
reapeat (Z = Z+X
X = X+1)
Until (X=6)

8. The output will be 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.


Exercise 3.2

2. procedure SubStringSearch(FirstString, SecondString)


P = 0;
Success = false;
while (P + length of FirstString) <= (length of SecondString)
and Success = false) do
[N = 1;
while (P + Nth character in SecondString =
Nth character in FirstString) do
(N = N + 1);
if (N = length of FirstString)
then (Success  true)))
P =P + 1]

57

You might also like