Anda di halaman 1dari 44

POLYTECHNIC OF NAMIBIA

SCHOOL OF INFORMATION TECHNOLOGY


DEPARTMENT OF BASIC COMPUTER STUDIES

Introduction to Algorithm Design


(ADI111S)

compiled by
Bernd Schulz

SIT / BCS ADI111S V. 2.3 31/08/2010


2

Table of Contents

Why learning Algorithm Design? ................................................................................................... 3


Problem Solving .............................................................................................................................. 4
What is an Algorithm? .................................................................................................................... 6
Where do Algorithms come from? .................................................................................................. 9
Steps in program development ...................................................................................................... 11
Program data ................................................................................................................................. 13
Pseudocode .................................................................................................................................... 15
Developing an Algorithm .............................................................................................................. 19
Searching and Sorting ................................................................................................................... 35
Algorithm representation............................................................................................................... 39

SIT / BCS ADI111S 31/08/2010


3
Why learning Algorithm Design?
Imagine you have been assigned to write a computer program that has to perform a particular
task. When you immediately sit down in front of your PC to type the program, you wouldn‟t
know what to type, even if you were a good programmer. The reason is that initially you are
facing the problem of how to solve that particular task. That‟s why there are a couple of things
that have to be done before starting programming.

First of all you have to understand the problem, which has to be solved. Then you have to find a
solution to the problem. Next, you will write down your solution in form of step-by-step
instructions, called an algorithm. And last, most importantly, you will test whether your solution
does really solve the problem. If not, then you have to find another solution. And even if your
solution works, you might spend a few minutes thinking if there is perhaps a better, faster,
simpler solution. Only then you can sit down at your PC and translate your step-by-step solution
into a programming language.

Therefore, the professional approach to writing a computer program is, to first take a pencil and
a piece of paper and go through the steps described above.

During the course “Introduction to Algorithm Design” we will learn and practice the first four
steps of professional program development. The coding of programs and the management of the
development process will be taught during the second semester in the courses “Introduction to
Programming” and “Introduction to Software Engineering”.

Program Development

Identify the problem

Outline a solution

1st Semester:
Develop the outline into Algorithm Design
an algorithm

Test the algorithm for


correctness

Code the program

Run and test the program

Document and maintain


the program

SIT / BCS ADI111S 31/08/2010


4
Problem Solving
Problem solving is the process of transforming the description of a problem into the solution of
that problem by using our knowledge of the problem domain and by relying on our ability to
select and use appropriate problem-solving strategies, techniques, and tools.

This is a very formal definition of “problem solving”. It means in other words that after having
understood a problem we have to use our knowledge and experience to find a workable solution.
Unfortunately, problem solving cannot be learned by heart or studied from books, because most
problems are different from each other, and a solution for one will most likely not solve another
one. Therefore, it needs practice and experience to successfully solve all kinds of problems.
Luckily, there is help available in form of strategies and tools.

The nature of IT courses will consistently present you with problems that you are expected to
find solutions for. Improving your problem solving skills can help you become more successful
in your courses. But problem solving is by no means restricted to your studies. Starting from the
time you wake up to the time you go to sleep, you are faced with all kinds of problems and must
find the solutions. You must understand the problem, break it down, in some cases understand
the causes and factors that affect the problem, and come up with solutions and select the best.

It comes as no surprise that the general approach to solving any problem is very similar to the
first steps of program development:

How to tackle a problem:

1. Understand and isolate the problem


2. Brainstorm for ideas to solve the problem
3. Design a solution that might work
4. Test your solution to see if it will work
5. Assess whether the solution is good enough or needs improvement

Problem  Suggestions / Brainstorming  Solutions

You can approach a problem in three ways:

1. Start at the beginning


This is the usual way of thinking through a problem from the start to the end.
2. Start at the final solution
Look at the solution that you want to achieve, and then figure out how to get there. This
method is often applied to social or other very complex problems, but also used in
mathematics.
3. Start in the middle
This approach is especially useful if a problem has several threats of which some can be
solved in parallel. Therefore, it is often applied to problems related to project
management.

There are a number of aids and tools available to assist you in problem solving:

- Simplify or break it down


Necessary in case of large or complex problems
- Use a formula
Standard method for problems in e.g. mathematics or physics

SIT / BCS ADI111S 31/08/2010


5

- Make a diagram or sketch


For many people it is easier to think about a problem when it is visualised in form of a
simple picture
- Make a chart, list or table
Absolutely essential for scheduling tasks and for all problems that involve a large amount
of data
- Eliminate
Look at all potential solutions and then cancel one by one those which do not work
- Look for patterns
Check whether there are repeating characteristics, or whether the problem is similar to a
problem for which you already have a solution
- Guess
If nothing works, have a guess. You might prefer this method whenever a formal
approach like e.g. “Eliminate” would take too much time, or if the problem is very
complex. After two or three guesses you might be lucky and your testing of the solution
shows that you found a valid solution.

SIT / BCS ADI111S 31/08/2010


6
What is an Algorithm?

Informally, an algorithm is a step-by-step solution to a problem


or
a set of instructions that defines how a task is performed.

For example there are algorithms


for constructing model airplanes (in form of instruction sheets),
for playing music,
for performing magic tricks.

Example: Oil Check

Let‟s assume that you are asked to check whether there is enough oil in a car. If not, then you
should refill oil, check again, and repeat the process, until the oil level is correct.

A simple algorithm might look as follows:

1. Open the bonnet of the car.


2. Check whether there is enough oil.
3. If there is not enough oil, fill in a can of oil, and return to step 2.
4. Else, if there is enough oil, close the bonnet.

Those of you who have experience with cars will start doing the job straight away. Many others
will already have problems opening the bonnet. Did you know that you have to get inside the car
and find a little lever somewhere under the dashboard to unlock the bonnet? Do you know how
to check the oil level of an engine? Do you know where to find the probe for the oil?

Thus, we see that the detail of the step-by-step instructions is dependent on the intelligence,
knowledge, skills or abilities of the person or machine executing the algorithm. That also applies
to computers. For instance it is important to know if a computer is able to solve the expression v²
or if you have to write v * v instead. Always keep in mind that computers are machines. They are
extremely fast, but incredibly stupid. They will do exactly what the programmer told them to do.
If the programmer‟s step-by-step instructions are wrong, the computer will neither realise it nor
will it care.

For those who don‟t know much about car maintenance step 2 of our example probably has to be
extended:

2.1. Locate the oil probe (a stick with a ring shaped handle) at the right side of the engine.
2.2. Pull out the probe.
2.3. Clean the probe with e.g. paper, tissue or a cloth.
2.4. Reinsert the probe.
2.5. Pull out the probe again and check the oil level.
2.6. Reinsert the probe.

Now some may still ask: “How will I know whether the oil level is correct?” Then we would
have to explain step 2.5 more in detail.

SIT / BCS ADI111S 31/08/2010


7
If all instructions are sufficiently explained, then everybody should be able to check the oil level.
Or are there still problems?

What will happen if you follow the instructions in the wrong sequence? Most probably you will
not be successful. You can‟t check the oil level before you opened the bonnet, or located and
pulled out the probe. Hence, the correct order of the single instructions of an algorithm is very
important.

What will happen if you didn‟t get the keys of the car and the car is locked? You won‟t get
inside to be able to release the bonnet. Or what if you don‟t have a can of oil to top up when the
oil level is too low? Hence, it is required that the instructions of an algorithm are executable, i.e.
the user or the machine must be able to carry them out.

Let‟s go back to the very first 4-step algorithm for checking the oil. The experts might have
started already with the job. But does the algorithm mention, which oil level has to be checked?
My car is a 4-by-4 and has three different kinds of oil: engine oil, gear oil, and hydraulic oil for
the power steering. And if we agree on engine oil, what kind of engine oil has to be refilled if
necessary? There are different levels of quality, and a diesel engine requires another type of oil
than a petrol engine. Hence, the instructions of an algorithm are not allowed to be confusing or
leave room for different interpretations (ambiguous). Instead, they must be understandable and
absolutely clear (unambiguous).

It seems that our original definition of an algorithm has to be extended by adding our
observations:

An algorithm is an ordered set of unambiguous, executable instructions.

Another important feature of an algorithm is that the number of steps must be limited; i.e. the set
of instructions must be finite. After all, the user or machine cannot work through an endless list
of instructions.

But limiting the number of instructions is not good enough. What happens in our example
algorithm for checking the engine oil, if the engine has a leak and the oil is running out
underneath the car? Assuming that you have enough cans of oil available you would continue
checking and refilling until you are standing in oil ankle deep. Even so the algorithm has only a
handful of instructions (i.e. it is finite), the activity of checking and refilling the oil will never
come to an end. Thus, we have to insert additional instructions like

3.1. If there is not enough oil and


3.1.1. if the number of cans refilled is 3 or less, then fill in a can of oil, and return to
step 2.
3.1.2. else, if the number of cans refilled is more than 3, then stop the activity and report
a problem.

With these additions we have arrived at the final version of our definition:

An algorithm is a finite, ordered set of unambiguous,


executable instructions that directs a terminating activity.

SIT / BCS ADI111S 31/08/2010


8
Why bother writing an algorithm?

For your own use in the future – You don't have to rethink the problem.
So others can solve the problem, even if they know very little about the principles of how
the solution was derived.

Before a machine can perform a task, an algorithm for performing that task must be discovered
and represented in a form that is compatible with the machine.
A machine-compatible representation of an algorithm is called a program.
Programs, and the algorithms they represent, are collectively referred to as software, in contrast
to the machinery itself, which is known as hardware.

An algorithm is not the same as a program:

A program is written in some particular programming language, whereas an algorithm


might be written in any language.
An algorithm is more like the idea behind a program, but it is the idea of the steps the
program will take to perform its task, not just the idea of the task itself. Each meaningful
program is based on some kind of algorithm.

SIT / BCS ADI111S 31/08/2010


9
Where do Algorithms come from?
The study of algorithms began as a subject in mathematics. The search for algorithms was a
significant activity of early mathematicians. Their goal was to find a single set of instructions
that could be used to solve any problem of a particular type.

The ancient Greeks were famous for developing plenty of important mathematical algorithms
several hundred years before Christ (B.C.)

Euclid
Born: 325 B.C.
Died: 265 B.C.

Example: The Euclidean Algorithm to find the Greatest Common Divisor (GCD) of two
whole numbers.

1. Assign M and N the value of the larger and the value of the smaller of the two positive
whole input values, respectively.

2. Divide M by N and call the remainder R.

3. If R is not 0, then assign M the value of N, assign N the value of R and return to step 2,
otherwise the greatest common divisor is the value currently assigned to N.

Finding the GCD of 24 and 9

M N R
24 9
6
9 6
3
6 3
0

=> 3 is the GCD of 24 and 9.

(Source: Robertson, L.A., 2007, Simple Program Design, 5th Edition, Thomson)

The idea behind algorithms

Do we need to know the theory that Euclid applied to come up with this algorithm in order to use
it ourselves?
What intelligence is required to find the GCD using this algorithm?

SIT / BCS ADI111S 31/08/2010


10
Once an algorithm behind a task has been discovered:
It is not necessary for the user to understand the principles.
The task is reduced to simply following the instructions.
Intelligence is "encoded into the algorithm".

This means that the real challenge in program development lies in finding the required algorithm.
Coding this algorithm into a programming language is basically a relatively simple task.

The steps of the algorithm don't have to be filled in in complete detail, as long as the steps are
unambiguous and it is clear that carrying out the steps will accomplish the assigned task. Of
course an algorithm can only be expressed as a program if all the details have been filled in,
because a machine cannot guess or make assumptions.

So, where do we get an algorithm from?

Usually, algorithms have to be developed, often with a lot of thought and hard work. Skill in
algorithm development is something that comes with practice, but there are techniques and
guidelines that can help.

For most known common problems algorithms have been developed already, and they are
available in algorithm manuals or on the Internet.

SIT / BCS ADI111S 31/08/2010


11
Steps in program development
We already saw that there are 7 basic steps in the development of a program:

1. Identify the problem


2. Outline a solution
3. Develop the outline into an algorithm
4. Test the algorithm for correctness
5. Code the algorithm into a specific programming language
6. Run the program on the computer
7. Document and maintain the program

1. Identify the problem

It involves carefully reading and re-reading the problem until you understand completely what is
required. Sometimes it might become necessary to ask questions or to do a bit of research to get
more information. At this stage the problem can be divided into three separate components
namely:

the inputs
the outputs, and
the processing steps to produce the required outputs

PROCESS
Input Output

2. Outline a solution

Break the problem into smaller tasks or steps, and establish an outline solution.
Usually a rough draft of the solution may include:
o The major processing steps involved,
o The major subtasks (if any),
o The major control structures (e.g. repetition loops),
o The major variables and record structures, and
o The mainline logic.

3. Develop the outline into an algorithm

The solution outline developed in step 2 is then expanded into an algorithm: a set of precise steps
that describes exactly the tasks to be performed and the order in which they are to be carried out.
We will be using pseudo code (a form of structured English) to represent the solution algorithm.
Flowcharts and Nassi-Schneidermann diagrams will also be introduced as graphical methods of
representation.

4. Test the algorithm for correctness

One of the most important steps in the development of a program, often referred to as desk
checking. The main purpose of this step is to identify major errors early, so that they may easily
be corrected. It involves walking with test data through the logic of the algorithm exactly as a
computer would, keeping track of major variables on a sheet of paper.

SIT / BCS ADI111S 31/08/2010


12

5. Code the algorithm into a specific programming language

Only after all design considerations have been met in the previous four steps, and after you tested
whether your algorithm is really working you should actually start coding the program in your
chosen programming language.

6. Run the program on the computer

This step uses a program compiler / interpreter and programmer-designed test data to machine
test the code for syntax and compilation errors (those detected at compile time) and run-time
errors (those detected at run time). At this stage there are hopefully no logic errors anymore,
because they ideally should have been eradicated in step 4.

7. Document and maintain the program

Even so often neglected, program documentation is an important ongoing task from the initial
problem identification to the final test results. Documentation involves both external
documentation (such as hierarchy charts, the solution algorithm, and the test data results), and
internal documentation that may have been coded in the program.
Program maintenance refers to changes which may need to be made to the program throughout
its life cycle.

SIT / BCS ADI111S 31/08/2010


13
Program data

Variables and Constants

A variable name is the name given to a group of memory cells, designed to store a particular data
item. The contents of the memory cell of a variable may change during program execution.

The contents of the memory cell of a constant remains the same during program execution.

Elementary data items

Integer: Set of whole numbers, which may be positive, negative or zero

Real: Set of numbers, positive, negative or zero, which may include values before or
after a decimal point (floating point numbers).
Real numbers require more memory space than Integers, and the processing of
Real numbers is much slower than Integer operations.
Character: Representing the set of characters on the keyboard plus some special characters.

Boolean: Logical variable, representing a control flag or switch, which may contain one of
only two possible values: true or false (1 or 0).

The elementary data items are only able to store one piece of data, e.g. one number or one
character. To store groups of data, e.g. names, sentences, lists, or tables, the information can be
grouped in data structures:

Data structures

File: Collection of data stored in a structured form; often organised in records.

Record: Collection of data items or fields that all bear some relationship with each other.

Array: Data structure made up of a number of variables or data items of the same data
type, accessed by the same name. Access to individual items in the array is
achieved by using an index or subscript beside the name of the array.

String: Collection of characters which may be constant or variable.

Meaningful Names for Variables

A name for a variable is simply a method of identifying a particular memory location in the
computer. The uniqueness of a name will differentiate it from other locations.
Often a name describes the type of data stored. The name of a variable should reflect its contents.
Most programming languages allow about 255 characters for a variable name. Some
programming languages are case sensitive, others are not.

When using several words for a variable name, then separate them either by underscores “_” or
by capital letters, because usually spaces are not allowed within a name, e.g.

SIT / BCS ADI111S 31/08/2010


14

not allowed: student postal address (contains spaces)


bad: studentpostaladdress (difficult to read)

O.K.: student_postal_address
O.K.: StudentPostalAddress

Declaration and Initialisation

It is good practice to declare and initialise variables at the beginning of a program.

Declaration: Defining the name and the type of data items, e.g.
Integer: counter Real: number1, number2, pi String: name

Initialisation: Assigning an initial value (start value) to variables or constants, e.g.


counter = 0 number1 = 0.0 number2 = 1.0 pi = 3.142 name = “Alice”

Most modern programming languages make it compulsory for the programmer to declare all
variables used.

Declaring your variables fulfils two purposes:

1. The computer has to know what data items to expect and how much memory space to reserve
2. For yourself it is important to clearly know which variables of what type you are going to use
for what purpose

Validation

It is good practice to validate all input data before processing. As the name suggests, validation
will ensure that the data entered can be used for processing without causing errors.

Data can be validated for:

correct type (e.g. integer, string, etc.)


correct range (e.g. months from 1 to 12, age from 0 to 130)
correct length (e.g. student number has a maximum amount of 9 digits)
completeness (e.g. when filling in on-line forms)
correct date (format of date and time, e.g. 10/03/2009, 14:30)

SIT / BCS ADI111S 31/08/2010


15
Pseudocode
The word Pseudocode contains the term “pseudo” from Greek pseudos, which means “lie, fake,
bogus”.
Pseudocode is a standard way of representing algorithms. It is a notational system in which ideas
can be expressed informally during the algorithm development process. The objective is to
consider issues of algorithm development and representation without being restricted to a
particular programming language.
The idea behind pseudocode is to use an unambiguous, consistent, concise notation for writing
standard algorithm structures.

When designing an algorithm, keep in mind that the set of instructions will eventually be
performed by a computer. Therefore, use words and phrases in the pseudocode, which are in line
with the basic computer operations so that the translation from the pseudocode to a specific
programming language will be quite simple.
Each operation can be represented as a straight forward English instruction, with keywords and
indentation to signify a particular control structure

Six basic computer operations

A computer is able to perform just six different types of operation:

1. Input
2. Output
3. Performing arithmetic
4. Assigning a value
5. Comparing two variables and selecting one of two alternative actions
6. Repeating a group of actions

1. A computer can receive information (Input)

When a computer is required to receive information or input from a particular source the
verbs Read and Get are used:
Read: input from a record of a file
Get: input from the keyboard.

Examples:

READ number_1, number_2 GET student_name

At this stage it is not necessary to specify the source of data, as this information is not
required until run time.

2. A computer can give out information (Output)

When a computer is required to supply output to a device, the verbs Print, Write, Put,
Output, or Display are used.
Print: output to a printer
Write: output to a file
Display: output to a screen

SIT / BCS ADI111S 31/08/2010


16

Examples:

PRINT “Program Completed” WRITE customer record to master file

DISPLAY “End of data”

In each example, the data to be put out is described concisely using mostly lower-case letters

3. A computer can perform arithmetic


Most programs require the computer to perform some sort of mathematical calculation or
formula, and for these, a programmer may use either actual mathematical symbols or the
words for those symbols.
For instance, the same pseudocode instruction can be expressed as either of the following:

ADD number to value

sum = value + number

The verbs Compute or Calculate are also available, e.g.: Compute C = (f – 32) * 5/9

(Remark: The formula transforms Degrees Fahrenheit to Degrees Celsius)

However, the use of these verbs is redundant, because the formula already fulfils the purpose.

When writing mathematical calculations for the computer, the „order of operations‟ should be
considered:

1. The first operation carried out will be any calculation contained within parentheses.
2. Next, any multiplication or division, as it occurs from left to right, will be performed.
3. Then, any addition or subtraction, as it occurs from left to right, will be performed.

4. A computer can assign a value to a variable or memory location


There are three cases where you may write pseudocode to assign a value to a variable or
memory location:

1. To give data an initial value in pseudocode, the verbs Initialise or Set are used.
2. To assign a value as a result of some processing, the symbol ‘=’ is written.
3. To keep a piece of information for later use, the verbs Save or Store are used.

Examples:

SET student_count to 0

Total_price = cost_price + sales_tax

STORE customer_num in last_customer_num

The equal symbol „=‟ has been used to indicate the assignment of a value as a result of some
processing.

Be aware that the symbol “ = “ is not identical with the “ = “ known from mathematics !

SIT / BCS ADI111S 31/08/2010


17

In mathematics the expression counter = counter + 1 doesn‟t make any sense at all, but in
programming it is perfectly valid. To understand its function, remember what a variable is: it
is a named memory cell. Therefore, in the example the processor would take the value stored
in the memory cell “counter”, add 1, and then write the new value back into the memory cell
“counter”.

5. A computer can compare two variables and select one of two alternative actions
An important computer operation is the ability to compare two variables (or values) and then,
as a result of the comparison, select one of two alternative actions.

To represent this operation in pseudocode, special keywords are used: IF, THEN, and ELSE.

The comparison of data is established in the IF clause, and the choice of alternatives is
determined by the THEN or ELSE options. Only one of these alternatives will be performed.

Example:

IF study_mode = “part_time”
THEN ADD 1 to part_time_count
ELSE ADD 1 to full_time_count
ENDIF

In this example the study mode of the student is investigated, with the result that either the
part-time counter or the full-time counter is increased
Note the use of indentation to emphasize the THEN and ELSE options, and the use of the
delimiter ENDIF to close the operation.
Also note that the example only works, if there are just two types of study modes. When we
also consider distant students, then the program would give wrong results, because the distant
students would increase the full-time counter. (Distant students are not part-time, therefore
the program will count them in the ELSE branch.)

6. A computer can repeat a group of actions

When there is a sequence of processing steps which needs to be repeated, two special
keywords, DOWHILE and ENDDO, may be used in pseudocode.
The condition for the repetition of a group of actions is established in the DOWHILE clause,
and the actions to be repeated are listed beneath it, terminated by ENDDO.

Example:

DOWHILE student_total < 50


READ student record
PRINT student_name, student_address to report
ADD 1 to student_total
ENDDO

Indentation

SIT / BCS ADI111S 31/08/2010


18

Indentation often enhances the readability of a program. For example, the statement

IF (item is taxable)
THEN
IF (price > limit)
THEN (pay x)
ELSE (pay y)
ENDIF
ELSE (pay z)
ENDIF

is easier to understand than

IF (item is taxable) THEN IF (price>limit) THEN (pay x) ELSE (pay y) ENDIF ELSE
(pay z) ENDIF

For the computer the form of notation usually does not make any difference, but for the human
being it is of major advantage to write algorithms and programs in an easily readable and
understandable style.

SIT / BCS ADI111S 31/08/2010


19
Developing an Algorithm

The Structure Theorem

It is possible to write any computer program


by using only three basic control structures:
1. Sequence
2. Selection
3. Repetition

1. Define the problem


2. Design a solution algorithm
3. Check the solution algorithm

Before you start designing your algorithm, it is extremely helpful to divide the problem into
three different components:

PROCESS
Input Output

While reading the problem description, identify the components:

 Input / Output Data, described by nouns and adjectives


 Processes, described by verbs and adverbs

Example: Add three numbers

A program is required to read three numbers, add them together, and print their total.

Nouns & adjectives Verbs & adverbs


Data: three numbers, total Processing: read, add, print

The components are then listed in a table, called Defining Diagram.

Defining Diagram

Input Data Processes Output Data

o READ three numbers


o three numbers
o ADD the numbers o total
o total
o PRINT the total

SIT / BCS ADI111S 31/08/2010


20

Note that no verbs like read or print may appear in the Data columns. Verbs are action words,
and therefore they can only be found in the Processing column.

At this stage it is a good idea also to think about the control structures that will be required
(sequence, selection, repetition), and about any formulas that might be necessary.

When you have finalised your Defining Diagram and thought about the two issues mentioned
above, then your algorithm is already halfway done. The input and output data will tell you, what
variables you have to use. You can use this knowledge directly for your declaration of variables.
However, while writing the algorithm it may become necessary to use more variables (e.g.
counters), which might not necessarily appear in the Defining Diagram, because they are neither
used for input nor for output. The processes tell you, what your algorithm has to do. Usually they
are also structured in the way input – process – output, i.e. first you read or get, then you do the
data processing (calculations, sorting, searching, etc.), and at the end there comes the printing,
saving, or displaying.

Be as thorough as possible when setting up your Defining Diagram, because experience shows
that mistakes in the Defining Diagram will consequently lead to mistakes in your algorithm.

Checking the Solution Algorithm

The testing of your algorithm is one of the most important steps in program development. You
should only start coding your algorithm into a programming language, if you are pretty sure that
your algorithm is correct. Otherwise, if later your program does not work, fault finding will
become very difficult, because you won‟t know whether you made a logical error in your
algorithm or whether you made a mistake while coding.

The testing of your algorithm is done with a pencil and a sheet of paper at your desk, hence the
name Desk Checking.

At this stage there are three different types of possible mistakes:

Logical error: main purpose of desk checking, usually easily detected


Run time error: an error that might only occur under certain circumstances while the
program is running on a computer (e.g. a division by zero, or endless
loops); often difficult to detect with a desk check
Syntax error: typing mistake (e.g. PRIND), or an instruction which is unknown to the
language compiler / interpreter; almost impossible to detect by desk
checking

How to do a desk check:

1. Choose two to three simple input test cases that are valid. Include special cases like zero
or negative values.
2. Establish for all of the test cases without using the algorithm what the expected result
should be.
3. On a piece of paper make a table of the relevant variable names from the algorithm. The
first column shows the line numbers of your algorithm.

SIT / BCS ADI111S 31/08/2010


21
4. Walk the first test case through the algorithm, line by line, keeping a step-by-step record
of the contents of each variable in the table as the data passes through the logic.
5. Check that the expected result established in Step 2 matches the actual result developed
in Step 4.
6. Repeat the walk-through process using the other test cases.

Often it is helpful to add columns for the different conditions of IF statements or loops (true /
false). A last column for data output (display, print) is also handy.

During desk check it is of utmost importance to do exactly what the single instructions of your
algorithm prescribe. While in Step 2 you act like a human being, in Step 4 you must act like the
processor, just following the instructions one by one. If you start dreaming, i.e. doing things
which you expect but which are not written in your algorithm, then your desk check will be in
vain, because this way you can never find any mistake.

Example: Average Speed

START
REAL: time, distance, av_speed
1 distance = 80
2 GET time
3 IF time > 0
4 THEN av_speed = distance / time
5 DISPLAY av_speed
6 ELSE DISPLAY “Error: time must be > = 0”
ENDIF
END

1. Test cases: 1. time = 2 2. time = 0 3. time = - 4

2. Expected results: 1. speed = 40


2. and 3.: “Error: time must be > 0”

3. and 4. First test case: time = 2, go line by line through the algorithm

# time distance av_speed time >0? Display


1 80
2 2
3 TRUE
4 40
5 40

5. Compare the output (Display) with the expected result in step 2: O.K.

6. Repeat steps 3 to 5 for all test cases

# time distance av_speed time >0? Display


1 80
2 0
3 FALSE
6 0 Error message

SIT / BCS ADI111S 31/08/2010


22
Compare with 2.: O.K.

# time distance av_speed time >0? Display


1 80
2 -4
3 FALSE
6 0 Error message

Compare with 2.: O.K.

All three test cases resulted in correct output. That does not necessarily mean that the
algorithm is 100% correct, but we can be confident that it does not contain any obvious
mistakes.

The Selection Control Structure

The selection structures are of very high importance, because they enable programs to branch
into different blocks of instructions depending on particular conditions. This way they provide a
kind of “intelligence” to programs.

There are several types of selection structures:

1. Simple selection
IF, THEN, ELSE, ENDIF
2. Simple selection with null false branch (no ELSE)
IF, THEN, ENDIF
3. Combined selection
IF using AND or OR, THEN, (ELSE), ENDIF
4. Nested selection (nested IF)
linear - non-linear
5. CASE structure

1. Simple selection
Example:

GET time
IF time > 0
THEN av_speed = distance / time
DISPLAY av_speed
ELSE DISPLAY “Error: time must be > 0”
ENDIF

SIT / BCS ADI111S 31/08/2010


23
2. Simple selection without ELSE

Example:

speed = distance / time


IF speed < 0
THEN DISPLAY “Attention: negative speed”
ENDIF
DISPLAY speed

3. Combined selection

Example 1:

IF (stud_mode = “distant”) AND (gender = “male”)


THEN INCREMENT male_dist_stud_counter
ENDIF

Example 2:

IF (stud_mode = “full-time”) OR (stud_mode = “part-time”)


THEN DISPLAY “This is not a distant mode student”
ENDIF

When using combined selection, make sure to give the variable name in each condition, i.e.
each condition must be a valid Boolean statement:

Wrong Example:

IF stud_mode = “full-time” OR “part-time”

4. Nested selection (nested IF)

4a. Linear (cascading) nested selection

Each ELSE immediately follows its IF statement

IF A
THEN … Linear nested IF structures can
ELSE … often be replaced by several
IF B separate IF structures
THEN …
ELSE …
ENDIF
ENDIF

4b. Non-linear nested selection

SIT / BCS ADI111S 31/08/2010


24

The ELSE statements may be separated from its IF

IF A
THEN …
Non-linear nested IF structures
IF B
can often be replaced by
THEN …
ELSE … combined selection using AND
ENDIF
ELSE …
ENDIF

Example 1: Linear selection

IF age < 6
THEN DISPLAY “Free of charge”
ELSE
IF age < 12
THEN DISPLAY “Half charge”
ELSE
DISPLAY “Full charge”
ENDIF
ENDIF

Example 2: Linear selection replaced by separate IF statements

IF age < 6
THEN DISPLAY “Free of charge”
ENDIF
IF (age >= 6) AND (age < 12)
THEN DISPLAY “Half charge”
ENDIF
IF age >= 12
THEN DISPLAY “Full charge”
ENDIF

Example 3: Non-Linear selection

IF stud_mode = “full-time”
THEN
IF stud_program = “IT”
THEN
IF gender = “female”
THEN INCREMENT female_IT_FT_counter
ELSE INCREMENT male_IT_FT_counter
ENDIF
ENDIF
ENDIF

SIT / BCS ADI111S 31/08/2010


25
Example 4: Non-Linear selection replaced by combined selection

IF (stud_mode = “full-time”) AND (stud_program = “IT”) AND (gender = “female”)


THEN INCREMENT female_IT_FT_counter
ENDIF

Extreme care must be taken when using an ELSE case in connection with the „AND‟
operator: in most cases the result will be wrong!

Wrong Example:

IF (stud_mode = “full-time”) AND (stud_program = “IT”) AND (gender = “female”)


THEN INCREMENT female_IT_FT_counter
ELSE INCREMENT male_IT_FT_counter
ENDIF

In this wrong example the male_IT_FT-counter will not only count all male IT full-time
students, but also all students who are not full-time or who are not studying IT, because
the ELSE statement handles all those cases in which the combined statement is false.
Remember: An AND statement is already false, when at least one element is false.

To handle the male_IT_FT_counter in our example correctly, the whole combined AND
statement has to be repeated:

IF (stud_mode = “full-time”) AND (stud_program = “IT”) AND (gender = “female”)


THEN INCREMENT female_IT_FT_counter
ELSE
IF (stud_mode = “full-time”) AND (stud_program = “IT”) AND (gender = “male”)
THEN INCREMENT male_IT_FT_counter
ENDIF
ENDIF

Remark: Instead of „ELSE‟ one could also use two separate IF statements.

5. The CASE structure

The CASE structure is a simplification of the linear (cascading) nested IF structure. It is


much easier to implement and to understand than a nested selection. However, not all
programming languages support the CASE structure.

CASE OF single variable


value_1: statement block_1
value_2: statement block_2
value_3: statement block_3
: :
value_n: statement block_n
value_other: statement block_other
ENDCASE

SIT / BCS ADI111S 31/08/2010


26
Example:

CASE OF stud_mode
“FM”: DISPLAY “Full-time mode student”
INCREMENT fm_counter
“PM”: DISPLAY “Part-time mode student”
INCREMENT pm_counter
“DM”: DISPLAY “Distant mode student”
INCREMENT dm_counter
other: DISPLAY “Error: invalid code”
ENDCASE

The example above replaces a much more complicated linear selection structure:

IF stud_mode = “FM”
THEN DISPLAY “Full-time mode student”
INCREMENT fm_counter
ELSE
IF stud_mode = “PM”
THEN DISPLAY “Part-time mode student”
INCREMENT pm_counter
ELSE
IF stud_mode = “DM”
THEN DISPLAY “Distant mode student”
INCREMENT dm_counter
ELSE
DISPLAY “Error: invalid code”
ENDIF
ENDIF
ENDIF

The Repetition Control Structure

In programming very often the need arises to repeat a set of instructions many times. For
instance to print out three test marks you can write PRINT mark1, PRINT mark2, PRINT
mark3. However, when you have to print fifty, hundred or even more marks then this way of
writing the PRINT statements becomes obviously unpractical. By using a so-called Repetition
Structure a computer can be ordered to automatically repeat a set of instructions.

A computer can process several millions, if not billions, of instructions per second. Therefore
even repeating large numbers of instructions will usually only take fractions of seconds.

Programmers usually call the repetition structure a loop, because while doing the repetition the
execution of the program is going around in circles.

Note: It is of utmost importance that all loops eventually come to an end, i.e. there must be a
mechanism to exit the loop if a certain condition arises. Otherwise, the program may “hang”
or “freeze”, meaning it will not respond to any input anymore because it will be busy doing the
repetitions for ever.

SIT / BCS ADI111S 31/08/2010


27
There are three basic types of repetition structures:

1. Leading decision loop


2. Trailing decision loop
3. Counted repetition

1. Leading decision loop

This type of loop is the most commonly used. It is called leading decision loop because
the condition, which determines whether the instructions shall be repeated or not, is
evaluated right at the beginning of the loop. The repetitions will stop as soon as the
condition becomes false.

DOWHILE condition is true


block of instructions
ENDDO

Example:

The following part of an algorithm will get 20 student numbers and 20 student marks
from the user, and then print a list in the form

1 student-no 1 student-mark 1
2 student-no 2 student-mark 2
… … …
20 student-no 20 student-mark 20

counter = 1
DOWHILE counter < = 20
GET stud_number
GET stud_mark
PRINT counter, stud_number, stud_mark
counter = counter + 1
ENDDO

2. Trailing decision loop

The trailing decision loop is called like this because the condition, which determines
whether the instructions shall be repeated or not, is evaluated at the end of the loop. As a
result the loop will always be executed at least one time before the decision to stop or to
continue can be made. In many cases this behaviour is not desirable, and therefore the
trailing decision loop is much less frequently used than the other types of repetition.
The repetitions will stop as soon as the condition becomes true. Hence the condition to
stop the loop is exactly the opposite of the condition in the leading decision loop.

REPEAT
block of instructions
UNTIL condition is true

SIT / BCS ADI111S 31/08/2010


28
Example: functionality similar to the previous example

counter = 1
REPEAT
GET stud_number
GET stud_mark
PRINT counter, stud_number, stud_mark
counter = counter + 1
UNTIL counter > 20

3. Counted repetition
Whenever one exactly knows how many repetitions will be required, then the use of the
counted repetition will be a good choice. This type of loop is simpler to implement than
the other types because it automatically counts the number of repetitions, and it stops as
soon as the required number has been reached. Thus, it is not necessary for the
programmer to initialise or increment the counting variable.

DO variable = start value TO stop value


block of instructions
ENDDO

Example: functionality similar to the previous examples

DO counter = 1 TO 20
GET stud_number
GET stud_mark
PRINT counter, stud_number, stud_mark
ENDDO

Reading from a file

Files may contain an awful lot of records, therefore the repetition structure is indispensable when
reading from a file. If we would know exactly how many records are in a given file, then we
could use a counted loop to read the records. However, usually the exact number is not known.
Luckily, there is a special record in a file, called the “End of File” record, abbreviated EOF. As
the name suggests, this record is always the last record in a file. Hence, while reading from a file
we just have to check whether the last record read is the EOF record; if yes, then we stop
reading, else we continue reading („DOWHILE NOT EOF‟).
There is only one little catch: to make it possible for the program to decide about the EOF when
the DOWHILE statement is executed for the very first time, we first have to do a READ
instruction. That is the reason why there are two READ instructions in such kind of algorithms:
one within the loop, which is doing all the work, and another one just in front of the DOWHILE
statement, which is only used once before entering the loop.

Note: In practice files are more complicated, but for the sake of keeping things simple we assume
that all files are terminated with the EOF record.

Example: Reading from a file and printing a list

SIT / BCS ADI111S 31/08/2010


29

START
STRING: stud_no, stud_name
READ stud_no, stud_name from file
DOWHILE NOT EOF
PRINT stud_no, stud_name
READ stud_no, stud_name
ENDDO
END

Terminating a loop with a Sentinel

Until now we terminated our loops either by using a counter or by checking for the end of a file
(EOF). Sometimes, however, neither of these methods is useful. Let‟s for instance assume, that
all IT students are asked to go to a computer and enter their student number and their age. The
data collected will then be printed. Of course we will apply a loop to get the data from the
keyboard and to print it. But how do we stop the loop when we are finished with capturing the
data? We don‟t know how many students are going to enter their data, therefore checking a
counter will not help. Neither do we have a file here to check for an EOF.
In cases like this we can define a special data item that signals to the program that we would like
to terminate the loop. Such a special data item is called a Sentinel. For our example we could for
instance say, that as soon as a student enters a 0 or a negative number as a student number the
loop will be terminated. The Sentinel should be a value that does not appear during normal data
entry, e.g. it could be a negative number, or a 0, or „999‟ when entering an age, or “X” when
entering a name.

Example: Sentinel

START
INTEGER: stud_no, age
GET stud_no
DOWHILE stud_no > 0
GET age
PRINT stud_no, age
GET stud_no
ENDDO
END

Arrays
In a normal variable you can only store one piece of data, e.g. one number or one character.
Remember that a variable is a memory cell, and if you write data into this cell the previous
content will be overwritten. However, quite often the need arises to store more than one value,
e.g. a large number of student names or test marks. We know already that for this purpose we
have to use one of the so-called data structures, either an array or a file.
Whereas a file can have different structures and can be saved on storage devices like hard disks
or USB sticks, an array has a rather fixed structure and only exists in a computer‟s main
memory. Consequently, all data of an array will be lost when its program is restarted or the
computer is switched off.
However, access to data stored in a computer‟s memory is more than a million times faster than
accessing data stored in a file on external storage devices.

SIT / BCS ADI111S 31/08/2010


30
An array is a collection of homogeneous data items in one single data structure.

“Homogeneous” means that an array may only contain data items of the same type, e.g.
INTEGER, REAL, CHARACTER, or BOOLEAN, and of the same length.
The single data items in an array are called elements.
An array uses the same variable name for all its elements. To distinguish the elements from each
other a so-called index or subscript is used, which is put in parenthesis behind the array name:
array_name (index). The index is an INTEGER number. In most programming languages it
starts with 0, in a few others with 1. Many programming languages use square brackets instead
of parenthesis. To be in line with our textbook, in this course we use parenthesis and start with
the index = 1.
One can imagine an array as a kind of table with one or many columns. If there is only one
column, the array is called one-dimensional. If there are two or more columns, it is called two-
dimensional or multi-dimensional. Multi-dimensional arrays require a separate index for each
dimension.
Example: a one-dimensional array

index array(index)
(1) element 1
(2) element 2
… …
(n) element n

Just like all other variables also arrays usually have to be declared at the beginning of a program
in order to enable the computer to reserve sufficient memory space for the array,
e.g. REAL: prices(100). This example informs the computer that the array with the name prices
will contain 100 elements of the type REAL.

Note that an expression like prices(100) may have two different meanings:
When used in the declaration, it means that the program has to reserve 100 memory locations
for an array with the name „prices‟.
When used in the body of a program, it means that you are addressing element no 100 of the
array „prices‟, e.g. prices(100) = 500.

Arrays can be used:


to load information into an array;
to process the elements of the array;
to store the results of processing into the elements of an array;
to read information from an array, e.g. to write to a report, or print a list.

To load information into an array the data is assigned to an element of the array by using the
equal sign in exactly the same manner as data is loaded into a normal variable, e.g.
index = 5
stud_mark (index) = 67 or stud_mark (5) = 67
Likewise, to retrieve data from an array it is handled in the same way as a normal variable, e.g.
index = 5
DISPLAY stud_mark (index) or DISPLAY stud_mark (5)

SIT / BCS ADI111S 31/08/2010


31
Note that instructions like WRITE, STORE, READ, etc are not used for arrays!

Because an array is only created with its declaration, like other variables it is empty at the
beginning of a program.
If the contents of an array must be saved at the end of a program, each element one after the
other has to be written to a file.

Paired Arrays
Arrays may only contain data items of similar type. If different types of related data have to be
stored, then several arrays have to be employed. However, because these arrays are containing
data that belongs together they are called “Paired Arrays”.
Paired arrays have the following characteristics:
they have the same number of elements, and
elements with the same index are corresponding.

Example: two paired arrays


While the array stud_name(index) contains STRINGS,
the array stud_mark(index) contains INTEGERS.

index stud_name(index) stud_mark(index)


(1) van Wyk 52
(2) Beukes 63
… … …
(n) Hamutenya 71

Examples of operations on arrays


Please note that all examples, except for numbers 2 and 4, assume that the arrays have been
filled (initialised) with data beforehand.
Example 1: Printing the elements of an array

START
INTEGER: index, marks(50)
DO index = 1 TO 50
PRINT marks(index)
ENDDO
END

Example 2: Filling an array with data from the keyboard

START
INTEGER: index, stud_mark, marks(50)
DO index = 1 TO 50
GET stud_mark
marks(index) = stud_mark
ENDDO
END
Remark: Regarding the purpose of the variable stud_mark refer to Example 4.

SIT / BCS ADI111S 31/08/2010


32
Example 3: Writing the elements of an array to a file

START
INTEGER: index, marks(50)
DO index = 1 TO 50
WRITE marks(index) to file
ENDDO
END

Example 4: Filling an array with data from a file

START
INTEGER: index, stud_mark, marks(50)
SET index to zero
READ stud_mark from file
DOWHILE (NOT EOF) AND (index <= 50)
marks (index) = stud_mark
READ stud_mark from file
index = index + 1
ENDDO
END

Remark: In this example two things are noteworthy:

1. The variable stud_mark is used to read a mark from the file. Only later the mark is loaded
from this variable into the array: marks(index) = stud_mark. The same approach applies when
getting data from the keyboard. In principal it would be possible to read the marks directly into
the array, e.g. „READ mark(index) from file‟. However, this can become very confusing when
reading different kinds of data (e.g. mark, name, student_number), and one must also take care
of handling the index correctly. Furthermore, not all programming languages allow the direct
way.

2. The DOWHILE loop contains two conditions: one taking care of the file and stopping the loop
when there are no more records to read (NOT EOF); the other one taking care of the array and
stopping the loop when the array is full (index <= 50). This is necessary because there may be
less or more than 50 records in the file. Only if you know exactly how many records are in the
file, then you may replace the DOWHILE loop with a counted repetition.

Example 5: Calculating the sum of elements

START
INTEGER: index, sum, marks(50)
SET sum to zero
DO index = 1 TO 50
sum = sum + marks(index)
ENDDO
PRINT sum
END

SIT / BCS ADI111S 31/08/2010


33
Example 6: Calculating the average of elements

START
INTEGER: index, sum, marks(50)
REAL: average
SET sum to zero
DO index = 1 TO 50
sum = sum + marks(index)
ENDDO
average = sum / 50 (or: average = sum / index)
PRINT average
END

Note that the average is calculated after the loop has been finished, because the average only
has to be calculated once when the complete sum is readily available.

Caution: When using the index to calculate the average, make sure that it really contains the
correct number of elements. Depending on the type of repetition, very often the index turns out to
be one count too high.

Example 7: Finding the largest and/or smallest element of an array

START START
INTEGER: index, largest, marks(50) INTEGER: index, smallest, marks(50)
largest = marks(1) smallest = marks(1)
DO index = 2 TO 50 DO index = 2 TO 50
IF marks(index) > largest IF marks(index) < smallest
THEN largest = marks(index) THEN smallest = marks(index)
ENDIF ENDIF
ENDDO ENDDO
DISPLAY largest DISPLAY smallest
END END

Why does the counted loop only start at index = 2? Because in the line before we made the first
element the largest / smallest. Therefore, it is useless to check right afterwards whether it is
greater or less than the largest / smallest – it can‟t be.

Both algorithms can be merged to find the smallest and the largest element concurrently:

START
INTEGER: index, largest, smallest, marks(50)
largest = marks(1)
smallest = marks(1)
DO index = 2 TO 50
IF marks(index) > largest
THEN largest = marks(index)
ENDIF
IF marks(index) < smallest
THEN smallest = marks(index)
ENDIF
ENDDO
DISPLAY smallest, largest

SIT / BCS ADI111S 31/08/2010


34
END

A special case is the search for the largest and/or smallest element in connection with paired
arrays. Let‟s say that we want to display the largest mark of our array marks(index) together with
the corresponding student number of a paired array stud_no(index). To be able to do so, our
algorithm has not only to catch the value of the highest mark, but also the index belonging to it.
This index is necessary to identify the student number which belongs to the highest mark.

index marks(index) index stud_no(index)


(1) 57 (1) 200912345 largest = 80
(2) 80 (2) 200954321
… … … … l_index = 2
(n) 46 (n) 200932154

START
INTEGER: index, largest, l_index, marks(50)
STRING: stud_no(50)
largest = marks(1)
DO index = 2 TO 50
IF marks(index) > largest
THEN largest = marks(index)
l_index = index
ENDIF
ENDDO
DISPLAY stud_no(l_index), largest or DISPLAY stud_no(l_index), marks(l_index)
END

SIT / BCS ADI111S 31/08/2010


35
Searching and Sorting
Very often it becomes necessary to search for a certain element in a long list, array or file, e.g. a
specific student number in a university‟s database.

Linear and Binary Search

Basically there are two methods of finding a data item in a list: one way is to start at the first
element and then to go one by one through the whole list, comparing each element with the one
we are looking for, until we have found the element or until we reach the end of the list. This
type of search is called a “Linear Search”. It is very simple, but will obviously take very long
when the list contains a lot of elements.

The other way is more sophisticated and much faster, but it requires sorting the list before
starting the search: We split the entire list into two parts and decide, in which part the required
element will be. Then we split this part of the list into two parts and decide again where to find
our element. We continue splitting and deciding, until we found the element, or discovered that
the element we are looking for is not in the list. This method of searching is called “Binary
Search”, because the list is repeatedly split into two parts.

Example 1: Finding a name in a telephone directory

To find for instance the name “Shilongo” in Windhoek‟s telephone directory, we will apply a
Binary Search. This is possible, because the names in the directory are listed in alphabetic order.
We split the Windhoek directory into two parts and decide that Shilongo will be in the second
half. Now we split this part again, and looking at the listed names we can decide in which part
we will find Shilongo. We continue doing so until we have found the right page listing the name
Shilongo.

If we had the task of finding a particular page number, we would apply the same method,
because the page numbers are also ordered in ascending sequence.

Example 2: Finding a telephone number in a telephone directory

If we have a telephone number from Windhoek and we would like to find the name of the owner,
we have no choice but to use a Linear Search, because the telephone numbers in the directory are
not sorted. Therefore, we have to start with the very first number and compare it with the one we
are looking for. Then we move to the next number, and so on. If we are unlucky, the owners
name starts with a letter at the end of the alphabet, meaning that our search will take hours.

Example 3: Linear Search: Finding a student number in an array

START
INTEGER: index, number, stud_no(50)
BOOLEAN: element_found
element_found = 0
index = 1
GET number

SIT / BCS ADI111S 31/08/2010


36
DOWHILE (NOT element_found) AND (index <= 50)
IF stud_no(index) = number
THEN element_found = 1
ELSE increment index
ENDIF
ENDDO
IF element_found
THEN DISPLAY stud_no
ELSE DISPLAY “Student number not found”
ENDIF
END

In this example the user types the student number s/he is looking for on the keyboard (GET
number), and the algorithm will compare all elements of the array with that particular number.
The algorithm employs a little trick to determine, whether the number has been found or not: a
Boolean variable element_found is set to 0 (false) or 1 (true) to indicate failure or success. After
the search routine is finished, the Boolean variable is used to decide on what has to be displayed.
A Boolean variable utilised in this way as an indicator is called a flag, and it is a method often
used by programmers.

Note that for Boolean variables it is not necessary to write „IF element_found = 0‟, or „IF
element_found = 1‟. Instead one may write „IF NOT element_found‟ for the false case, or simply
„IF element_found‟ for the positive case.

Sorting

To make searching easier and faster, data is often stored in a sorted manner, for instance ordered
by value or by alphabet. Sorting of data can be done using a number of different methods.

Bubble Sort

To sort a number of items in ascending order (i.e. smallest item first, largest item last) we can
have a look at the first pair of items and decide, whether they are in the right order. If yes, then
we move on to the next item. If not, then we will switch the two items around before moving on.
After we reached the last item, we will start again from the beginning. We repeat this process
until there is nothing to swap around anymore. When we have reached this stage, all our items
are in the right order.
If you apply this process to a list or an array, then you can see how the large elements “fall” to
the bottom of the list and the small elements through the repeated swapping climb to the top of
the list. This looks very much like bubbles in a glass of sparkling water striving to the top, hence
this method of sorting is called Bubble Sort.
Bubble sort is a rather simple and intuitive way of sorting data, but not a very efficient one.

Insertion Sort

When applying Insertion Sort we go through all elements of a list or array and look for one that
is out of order because it is too small. We remove this element, have a look at the first part of our
list, and insert the element at the correct position, where it belongs.

SIT / BCS ADI111S 31/08/2010


37
This method of sorting is much more efficient than a Bubble Sort, but also much more
complicated, because after removing a wrong element the list has to be rearranged to close the
gap, then we have to find the correct position where to insert the removed element, and again the
whole list has to be rearranged to make the necessary space to insert the element.

Selection Sort

For a Selection Sort we scan the whole list to find the smallest element. This element we remove
and put it into position number 1. Then we repeat the process, this time starting from the second
element. We continue like this, starting from element 3, 4 and so on, until we have reached the
end of the list. Again, this way of sorting is much more efficient than a Bubble Sort, but as
complicated as Insertion Sort because of the required frequent rearranging of the list.

Example: Bubble Sort

START
INTEGER: number, counter, marks(50), temp
BOOLEAN: elements_switched
number = 50
elements_switched = 1
DOWHILE (elements_switched) AND (number >= 2)
counter = 1
elements_switched = 0
DOWHILE counter <= number – 1
IF marks(counter) > marks(counter + 1) THEN
temp = marks(counter)
marks(counter) = marks(counter + 1)
marks(counter + 1) = temp
elements_switched = 1
ENDIF
counter = counter + 1
ENDDO
number = number – 1
ENDDO
END

This algorithm contains three special concepts:

1. The Boolean variable elements_switched is used as a flag, like earlier in Linear Search.

2. A temporary variable temp is introduced to swap the contents of the array elements
marks(counter) and marks(counter + 1). Without the use of a temporary variable, swapping
would be impossible.

(Imagine you have a glass of milk and a glass of orange juice in front of you. Now you get
the task to swap the contents of the glasses, i.e. the milk has to go into the glass where the
orange juice is, and the juice has to go into glass where the milk is. You will need a third
container, where you can temporarily put the milk or the juice, while swapping the content of
your glasses.)

SIT / BCS ADI111S 31/08/2010


38

2.
mark(counter) 55 42 mark(counter + 1)

1. 3.
55
temp

3. The algorithm employs a nested repetition structure, i.e. two loops within each other. The
first, “outer” loop is responsible for organising the whole sorting process and stopping it,
when no more switching of elements occurred. The second, “inner” loop does the actual
work of comparing and swapping the elements.

SIT / BCS ADI111S 31/08/2010


39
Algorithm representation
Until now we only used Pseudocode to write our algorithms. Pseudocode has the advantage of
having a structure that is rather similar to programming languages. Therefore, it is easy to
translate an algorithm written in Pseudocode into a programming language. On the other hand,
concepts like e.g. nested selections are often difficult to understand in Pseudocode.

However, there are other possibilities to represent algorithms.

Flowchart

A Flowchart is a graphical representation of an algorithm. Boxes containing instructions are


connected via lines or arrows indicating the data flow. You can actually take your finger and
follow the data through the processes of the algorithm from START to STOP. This way selection
and repetition structures become very clear.

Disadvantages of flowcharts:
they cannot easily be transformed into program code;
they require a lot of space and time to be drawn;
desk checking in the way it is done for Pseudocode is difficult.

Flowcharts are not only utilised for Algorithm Design, but they are very useful in many other
fields as well, like Business Management, Project Management, Process Engineering, or
Operations Research.

In order to draw a Flowchart one has to know a few basic symbols:

GENERAL PROCESS FLOW LINE

INPUT / OUTPUT OPERATION TERMINATORS

START
STOP

DECISION / SELECTION CONNECTOR OFF-PAGE CONNECTOR

SIT / BCS ADI111S 31/08/2010


40
There are more symbols for special purposes, e.g. for printing, displaying, magnetic storage
devices, etc., but they are optional and can be replaced with the general symbols, e.g. for input /
output operations.

Flowcharts drawn by hand tend to look rather untidy. There are stencils available for those who
use flowcharts quite often, or computer programs. Some software engineering tools are even able
to translate pseudocode or program code into flowcharts.

Another option is to use the drawing tool of an office program, as it was done for this script. On
the “Insert” ribbon of MS Office one may click “Illustrations: Shapes”, and then select
“Flowchart”. However, drawing large flowcharts with this tool is very tedious and takes a lot of
time.

A few basic rules for drawing flowcharts:


All flowcharts begin with a START-Terminator and end with at least one STOP-Terminator.
Each instruction gets its own box; only closely related instructions, e.g. declarations, may be
combined into one box.
All boxes have exactly one input and one output, except for terminators (either one input or
one output) or the decision box (one input and two outputs).
The decision box is the only box that has two outputs, therefore it is the only place where a
program may split into two branches.
Boxes are connected through straight lines, or for the sake of clarity through arrows.
Crossing of lines should be avoided.
All lines or arrows must end at a box, connector or terminator. “Loose ends” are not allowed.

Example 1: Simple sequence

START

REAL: speed, time,


distance

distance = 80

GET time

speed = distance / time

DISPLAY
speed

STOP

SIT / BCS ADI111S 31/08/2010


41

Example 2: Selection

START

REAL: speed, time,


distance

distance = 80

GET time

YES time >= 0 NO


?

speed = distance / time

DISPLAY DISPLAY
speed “Error: time
must be > 0”

STOP

SIT / BCS ADI111S 31/08/2010


42
Example 3: Repetition

START

REAL: speed, time,


distance

distance = 80

DISPLAY
GET time “Time must be > 0.
Please try again”

YES
time <= 0
?

NO

speed = distance / time

DISPLAY
speed

STOP

Note that flowcharts in this form are not able to display counted repetitions, because there is no
way to illustrate the automatic initialisation and incrementing of this type of loops. Therefore,
counted loops must be transformed into leading decision loops before drawing them as a
flowchart.

There is an alternative way of drawing repetition structures, but to avoid confusion we don‟t use
it in this course.

SIT / BCS ADI111S 31/08/2010


43
Nassi-Schneidermann Diagram

Nassi-Schneidermann Diagrams (short NS-Diagrams) combine the advantages of Pseudocode


and Flowcharts. They make use of Pseudocode, but condense it into a kind of table. This way,
repetitions or nested selections become clearer, and the representation of the algorithms don‟t
require much space.

NS-Diagrams do not require the keywords START or STOP because, if correctly drawn, the
algorithms always start at the first box and end at the last box.

Note: NS-Diagrams are named after their inventors, Dr Ike Nassi and Dr Ben Shneiderman.
There are different spellings of the name Schneidermann in literature. Presumably, the
originally German family Schneidermann emigrated to the USA, where the name got
“Americanized” to Shneiderman.

Example 1: Simple sequence

REAL: speed, time, distance

distance = 80

GET time

speed = distance / time

DISPLAY speed

Example 2: Selection

REAL: speed, time, distance

distance = 80

GET time

IF time > = 0
Yes No
speed = distance /
time DISPLAY “Error:
time must be > 0”
DISPLAY speed
DISPLAY “End of Program”

Note: The keyword ENDIF is unnecessary, because the selection ends where the two columns
become one again. If a selection does not contain an ELSE path (null false branch), then the box
in the “No-column” is simply left empty. In this case the column might also be made much
narrower to save space.

SIT / BCS ADI111S 31/08/2010


44

Example 3: Repetition (leading decision)

REAL: speed, time, distance

distance = 80

GET time

DOWHILE t < = 0

DISPLAY
“Time must be > 0.
Please try again”

GET time

speed = distance / time

DISPLAY speed

Note: The keyword ENDDO is unnecessary, because the repetition ends where the double line on
the left side ends.

Example 4: Repetition (trailing decision)

INTEGER: stud_no, index,


mark(100)
index = 1

PRINT
counter, stud_no,
mark(index)

increment index

UNTIL index > 100

PRINT “End of List”

Note: The keyword REPEAT is unnecessary, because the repetition starts where the double line
on the left side starts.

SIT / BCS ADI111S 31/08/2010

Anda mungkin juga menyukai