FP305 DATA STRUCTURE
Page 1
OVERVIEW OF ALGORITHM
ALGORITHM :
Algorithm is a set of steps for solving a particular problem using a certain method until the problem is solved. The word ALGORITHM is named as the name of Mathematician namely Al-Khawarizmi.
Page 2
OVERVIEW OF ALGORITHM
The roles of algorithm in problem solving :
i. Help a programmer to write efficient code and solve problems in optimal or near-optimal ways.
ii.
Help a programmer to structure the code, so that it can be
maintained more easily by encouraging a better design / implementation.
Page 3
OVERVIEW OF ALGORITHM
ALGORITHM can be developed using:
1. Pseudo Code(Similar to programming language) 2. Flow Charts Algorithm constructed in the form that is easily understood by humans to facilitate the review of the program logic flow.
However, the algorithm can not be understood by the computer.
Page 4
OVERVIEW OF ALGORITHM
1. Pseudo Code
Pseudo code is a sequence of steps that are written using natural language processing expository involved in solving
problems.
The main purpose of presenting problem solving in a logical
and systematic.
Page 5
OVERVIEW OF ALGORITHM
There are several ways of writing pseudo code is used, two
types will be introduced: i. Language statement
ii.
Programming language similar to language statement
Each step in problem solving are arranged one by one systematically. Each line of pseudo code represents a one-off measures implemented.
Page 6
OVERVIEW OF ALGORITHM
i.
Language statement
Algorithms written in natural language and has no specific regulations.
In this method the number given in the sequence of steps
is very important.
Page 7
OVERVIEW OF ALGORITHM
Example: an algorithm for preparing a cup of coffee
1. boil water 2. add a cup of coffee
3. if necessary sugar
3.1. add sugar 4. if necessary cream 4.1 add cream 5. put hot water into the cup
Page 8
OVERVIEW OF ALGORITHM
The order of implementation of measures in accordance
with written numbers. Suitable for a simple problem because there is no sequence
and structure of the complex.
Page 9
OVERVIEW OF ALGORITHM
a) Sequence structure:
1. beginning 2. step_1
3. step_2
: n. step_n n +1. end
Page 10
OVERVIEW OF ALGORITHM
b) The structure of the selection:
n. if the conditions n.1. step_n.1
n.2. step_n.2
: nn step_n.n
Page 11
OVERVIEW OF ALGORITHM
c) Structure jump:
n. move to the n
d) Repeat structure:
n. go to step before the n
Page 12
OVERVIEW OF ALGORITHM
ii.
Programming language similar to language statement
Disclosure of pseudo code is a simple algorithm in the English language that closely resembles the programming
language PASCAL - BUT it is NOT tied to any programming
language. The order of implementation of measures can be controlled by using some control structures. Keywords used to explain the structures of this control.
Page 13
OVERVIEW OF ALGORITHM
a) Sequence structure:
begin 1. beginning
2. step_1
3. step_2 : n. step_n n +1. end
Page 14
OVERVIEW OF ALGORITHM
b) The structure of the selection:
if syarat then code block true
else
code block is not true End if
Page 15
OVERVIEW OF ALGORITHM
c) Struktur ulangan:
repeat bhg_yg_perlu diulang
until syarat
Page 16
OVERVIEW OF ALGORITHM
while syarat do bhg_yg_perlu diulang
End while
Page 17
OVERVIEW OF ALGORITHM
Example: an algorithm for preparing a cup of coffee using programming language similar to language statement
Page 18
OVERVIEW OF ALGORITHM
begin add a cup of coffee if necessary sugar then add sugar end if if necessary cream then add cream
end if
put hot water into the cup end
Page 19
OVERVIEW OF ALGORITHM
There are three basic control structures in algorithm :
i.
ii.
Sequence
Choice
iii. Repeat or Loop
Page 20
OVERVIEW OF ALGORITHM
2. Flow Chart
Flow chart consisting of boxes containing various forms to indicate the operation to be done and the arrows to specify the sequence of
operations.
Page 21
OVERVIEW OF ALGORITHM
Flow chart symbols
Start Add Sugar Process Subroutine functions
End Terminator
Page 22
OVERVIEW OF ALGORITHM
Flow chart symbols
A 3
Decision
Connector at same page
Off-page connector
Page 23
OVERVIEW OF ALGORITHM
Flow chart symbols
Print
Input
Storage
Page 24
OVERVIEW OF ALGORITHM
Flowcharts provide a cup of coffee
Start
Boil a water
Put the coffee
25
Page 25
OVERVIEW OF ALGORITHM
26
Add sugar
Yes
Need sugar?
No Need creamer? Yes Add creamer
No
Put the hot water
End
Page 26
OVERVIEW OF ALGORITHM
Algorithm is built must have the following characteristics :
i.
ii.
Troubleshooting a finite or a finish
Each presented with a clear direction
iii. Algorithm has input to begin execution
iv. Algorithm has output as a result of the implementation
v. Algorithm can be implemented effectively
Page 27
OVERVIEW OF ALGORITHM
Two main factors used to measure the effectiveness of an
algorithm are :
i. ii. Running time Use of memory space
Page 28
EXERCISES
1. There is a program which will receive a pair of input numbers and print the message "SAME" if the two numbers
have the same value, the message "NUMBER ONE IS BIGGER
if the first number is greater than the second number and the message "NUMBER TWO IS BIGGER if the second number is greater than the first number.
Page 29
EXERCISES
What you have to do is:
i.
ii.
Write
an
algorithm
using
pseudo
code
in
language
statement, Write an algorithm using flow chart, and
iii. Write a program based on your flow chart
Page 30
EXERCISES
2. 3. 4.
Write pseudo code in language statement, to solve the
problem of finding an average of two numbers.
Define an algorithm. List six properties that an algorithm should possess.
Page 31