Algorithm (2210 comp sci)
Algorithm (2210 comp sci)
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
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
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
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:
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
24
Sequence Logic
25
Selection Logic
26
Selection Logic
27
Selection Logic
28
Iteration Logic
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
Examples:
37
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