Algorithm and
Pseudocodes
1
Algorithm
2
What is Algorithm…
Algorithm
A set of unambiguous instructions
for solving a problem or sub problem
in a finite amount of time using a
finite amount of data.
( an algorithm is an abstract idea)
3
What is Algorithm…?
By wikipedia definition:
▪ an algorithm is a sequence of finite
instructions, often used for calculation
and data processing.
▪ It is formally a type of effective method in
which a list of well-defined instructions for
completing a task will, when given an
initial state, proceed through a well-
defined series of successive states,
eventually terminating in an end-state.
4
What is Algorithm…
5
Developing an Algorithm
Two methodologies used to develop
computer solutions to a problem
Top-down design focuses on the tasks to
be done
Object-oriented design focuses on the
data involved in the solution
Important note: Algorithms are *not*
computer code
6
EXAMPLE
An algorithm is a description of a
method for accomplishing some
task. For example, an algorithm for
driving to a friend's house could be:
1. Find your keys
2. Get into the car
3. Start the engine
4. Put transmission into gear
etc...
7
EXAMPLE
For instance, find the largest number in a
list
Consider a list like this: 3,6,2,67,9,1
Clearly 67 is the largest number. One
algorithm that would find this is
store first list item in variable largest_item
For each item in the list
store item in variable current_item
if current_item > largest_item
then
largest_item = current_item
End
This algorithm takes the first item in the list and sets it to be
the largest. Then it loops through the other items and checks
8
Properties of Algorithm
1.Finiteness
The execution of a programmed
algorithm must be complete after a finite
number of operations have been
performed. Otherwise, we cannot claim
that the execution produces a solution.
9
Properties of Algorithm
2.Absence of Ambiguity
(uncertainty/doubt)
The representation of every step of an
algorithm should have a unique
interpretation which also understand by
the human.
It is convenient to deal with algorithms
presented in notational with sparse
detail:
▪ Example:
▪ Pseudo code 10
Properties of Algorithm
3.Sequence of Definition
The sequence in which the steps of the
algorithm are to carried out should be
clearly specified.
In algorithmic specifications, the
instructions are performed from top to
button, unless the instruction
themselves otherwise specified.
11
Properties of Algorithm
4.Input and Output Definition
Inputs – are the data items that is
presented in the algorithm.
Outputs – are the data items presented
to the outside world as the result of the
execution of a program based on the
algorithm.
An algorithm ought to produce at least
one output (otherwise, what use is it?...)
12
Properties of Algorithm
5.Effectiveness
It consists of basic instructions that are
realizable. This means that the
instructions can be performed by using
the given inputs in a finite amount of
time.
The instructions of an algorithm may
order the computer only to perform
tasks that is capable of carrying out.
13
Properties of Algorithm
6.Scope Definition
An algorithm applies to the following:
▪ Specific problem or class of problem
▪ The range of inputs has to be predefined
▪ The range determines the generality of the
algorithm.
14
How to express an
ALGORITHM?
Algorithms
can be expressed in
many kinds of notation, including:
Natural language
Pseudo Code
Flowcharts
Programming Language
15
Pseudocode
16
What is Pseudocode…?
“Pseudo” means imitation or false
and “code” refers to the instructions
written in a programming language.
Pseudocode is another programming
analysis tool that is used for
planning a program.
Pseudocode is also called Program
Design Language (PDL).
17
Pseudocode
Pseudo-code does not follow any
particular computer language. It lays
out the algorithm as a series of
statements written in English (or any
local language). Some statements will
test for some condition and branch to
different parts of the algorithm.
18
EXAMPLE
Psuedocode is an implementation of an algorithm
in a code-like format. For example, the above
algorithm ( slide # 7)
in psuedocode could look something like:
while(keys.location != "in your hand")
{
search_for_keys();
}
walk_to_car();
if(car.door == locked)
car.door.unlock();
engine.start();
... 19
Example. .
Write pseudo code for an algorithm that adds data
to an existing file.
1. Ensure the data to be added is present in
memory
2. Identify the file to be opened
3. Open the file
4. Move to the end of the file
5. Append the data from memory to the end of the
file
6. Save and close the file
As you can see, each line is an action to do
something i.e. each line has a verb that describes
the action : 'Ensure', 'Identify', 'Open', 'Move',
20
If more detail is needed the pseudo code can be expanded:
1. Ensure the data to be added is present in memory by
if data is present in memory then
carry on
else
load data into memory
2. Identify the file to be opened
3. Open the file
4. Move to the end of the file
5. Append the data from memory to the end of the file
6. Save and close the file
Notice that the word 'if' was used in section 1 above and the term 'else'. These terms
are called 'conditional' statements and they cause the algorithm to branch
depending on some condition being met. This kind of statement is called a
'selection' because it determines the next instruction to be carried out.
21
What is Pseudocode…?
By wikipedia definition:
Pseudocode is a compact and
informal high-level description of a
computer programming algorithm
that uses the structural
conventions of some programming
language, but is intended for
human reading rather than
machine reading.
22
Logical Structure of
Pseudocode
Pseudocode is made up of the
following logic structures that have
been proved to be sufficient for
writing any computer program:
Sequence Logic
Selection Logic
Iteration Logic
23
Sequence Logic
Itis used to perform instructions in a
sequence, that is one after another.
Thus, for sequence logic,
pseudocode instructions are written
in an order in which they are to be
performed.
The logic flow of pseudocode is from
top to bottom.
24
Sequence Logic
25
Selection Logic
It is used for making decisions and
for selecting the proper path out of
two or more alternative paths in
program logic.
It is also known as decision logic.
Selection logic is depicted as either
an IF...THEN or an IF...THEN...ELSE
structure.
26
Selection Logic
27
Selection Logic
28
Iteration Logic
It is used to produce loops when one
or more instructions may be
executed several times depending
on some of the conditions.
It uses structures called the
DO_WHILE,
FOR (FOR –NEXT) and the
REPEAT_UNTIL.
29
Iteration Logic
30
Iteration Logic
31
Iteration Logic
32
1. Write only one statement per line.
Each statement in your
pseudocode should express just
one action for the computer.
If the task list is properly drawn,
then in most cases each task will
correspond to one line of
pseudocode.
33
Rules for Pseudocode
Examples
34
Rules for Pseudocode
2. Capitalized initial keyword.
In the example above, READ and
WRITE are in caps.
There are just a few keywords we
will use:
▪ READ, WRITE, IF, ELSE, ENDIF,
WHILE, ENDWHILE, REPEAT,
UNTIL
35
Rules for Pseudocode
3. Indent to show hierarchy.
We will use a particular indentation
pattern in each of the design structures:
▪ SEQUENCE: keep statements that are
“stacked” in sequence all starting in the same
column.
▪ SELECTION: indent the statements that fall
inside the selection structure, but not the
keywords that form the selection
▪ LOOPING: indent the statements that fall
inside the loop, but not the keywords that
form the loop 36
Rules for Pseudocode
Examples:
37
Rules for Pseudocode
4. End multi-line structures.
▫ All the initial keyword must always in line
with the last or end of the structure.
5. Keep statement language independent.
▫ Resist the urge to write in whatever language
you are most comfortable with. There may be
special features available in the language you
plan to eventually write the program in; if you
are SURE it will be written in that language,
then you can use the features. If not, then
avoid using the special features.
38
Rules for Pseudocode
Insummary:
Write only one statement per line.
Capitalized initial keyword.
Indent to show hierarchy.
End multi-line structures.
Keep statement language
independent.
39
Standard for good
pseudocode…
• These are follows:
▫ Number each instruction.
This is to enforce the notion, “well-ordered
collection of ... operations.”
▫ Each instruction should be unambiguous.
It means the computing agent, in this case the
reader, should be capable of carrying out the
instructions. And also, each instruction should
be effectively computable (do-able).
▫ Completeness.
Nothing should be left out.
40
Advantage of
Pseudocode…
Following
are some of the
advantages of using pseudocode:
Converting a pseudocode to a
programming language is much more
easier than converting a flowchart.
As compared to flowchart, it is easier to
modify a pseudocode of a program logic
when program modifications are
necessary.
41
Limitations of
Pseudocode…
Italso suffers from some of the
limitations. These limitations are as
follows:
In the cases of pseudocode, a graphic
representation of program logic is not
available.
There are no standard rules to follow for
using a pseudocode. Different
programmers use their own style of
writing pseudocode and hence,
communication problem occurs due to 42
Calculation Symbols
To
symbolize the arithmetic
operators we use these symbols:
Note: There is a precedence or hierarchy
implied in this symbols.
43
Selection Symbols
When we have to make a choice
between actions, we almost always
base that choice on a test.
There is a universally accepted set of
symbols used to represent these
phrases:
44
Selection Symbols
45
Logical Operators
46
Logical Operators
47
THE END
48