You are on page 1of 48

Faculty Of Engineering and Technology

Department of Computer Science


Course: Data structure and Algorithm
(Lecture-2)

Sayed Mortaza Kazemi


Department of Computer Science
Email: sayedmortaza11@gmail.com
Mobile: +93(0) 795474969
Algorithm
• Well defined list of steps for solving a particular problem
• When we are writing a computer program we are generally
implementing a method that has been devised previously to
solve a problem – algorithm
• Algorithm is independent of a particular computer and the
software used for development

2
Cont
• Algorithm is general for all sorts of computers and
programming languages
• Same program written by different people give different
performance rate, all because of the algorithms behind the
logic of these programs
• algorithm is used to describe a problem solving method

3
Cont
• Algorithm is written independent of computer programs
• Algorithm is written in a language understandable to the
programmer and does not have strict syntax, statements, etc.
• The codes used for writing algorithm is called pseudo code or
algorithmic notations

4
Informal definition of an algorithm
used in a computer
Finding the largest integer
among five integers
Defining actions in FindLargest algorithm
FindLargest refined
Generalization of FindLargest
Time Space Tradeoff
• Time and space are the two major and primary variables measuring the
efficiency of the program
• Time is the amount of time used to complete a process
• Space is the amount in memory consumed by the process during its
processing
• The complexity of algorithm is directly measured by the amount of time
taken and memory used
• Normally an algorithm larger in size takes much time to process as
compared to the algorithm which is short in size

10
• So with the increase of the size of algorithm, processing time
also increases, and vice versa.

11
Algorithmic Notations – Step,exit
• An algorithm is executed statement by statement, one after
the other
• Each statement in algorithm has a step with or without step
number

– Last statement of the algorithm is EXIT, or STOP, or TERMINATE,


etc
• This statement ends the algorithm and has no impact the
processing

12
Algorithmic Notations - Comment
• Comments are useful hints for the reader of algorithm
• These play no role in the processing of algorithm
• In a computer program they completely ignored by the
compiler
– 20 [initialize the number to zero] x=0

13
Algorithmic Notations - Variable
• Variable is a data item that receives its value during
processing
• A variable is given an appropriate name
– 100 X=45
– 110 MAX=50
Algorithmic Assignment
A variable or a data item is given a value
with the help of assignment (=)
30 counter=1

14
Algorithmic Notations - Input
• Input is the process that provides data to the algorithm
• The data is termed as input data
• We can use any of the following statements for input
– READ, INPUT, ACCEPT, etc
– INPUT Variable1

15
Algorithmic Notations - Output
• The process by which algorithm gives out data.
• The data is termed as output data
• We can use any of the following statement for output
– OUTPUT WRITE DISPLAY PRINT etc
– PRINT Name

16
Algorithmic Notations – Selection
• Selection logic employs a number of conditions which lead to a
selection of one out of several alternative modules
– 10 IF condition, then
– [module a]
– [end if]

– 40 IF condition, then
– [module a]
– Else
– [module b]
– [end if]

17
Algorithmic Notations - Iteration
• Iterative logic makes a statement or group of statements to
repeat until a certain condition or for some specified number
of times
• 10 Repeat while condition
• 30 [module]
• 40 [end loop]

18
Example Algorithm
Algorithm (sum of two numbers). X, Y, Z
are three numbers
1. READ X
2. READ Y
3. SET Z=X+Y
4. OUTPUT Z
5. EXIT

19
Class Activity

What is the difference between


program and algorithm?

20
8.2

THREE CONSTRUCTS
Three constructs
The Flowchart
 (Dictionary) A schematic representation of a sequence of operations, as in a
manufacturing process or computer program.
 (Technical) A graphical representation of the sequence of operations in an
information system or program.
 Information system flowcharts show how data flows from source documents through the
computer to final distribution to users.
 Program flowcharts show the sequence of instructions in a single program or subroutine.
Different symbols are used to draw each type of flowchart.
Flowchart Symbols
Basic
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<5
0

PRINT PRINT
“PASS” “FAIL”

STOP
Example 2
 Write an algorithm and draw a flowchart to convert the
length in feet to centimeter.
Pseudocode:
 Input the length in feet (Lft)
 Calculate the length in cm (Lcm) by multiplying LFT with
30
 Print length in cm (LCM)
Example 2
Flowchart
Algorithm START

 Step 1: Input Lft


Input

 Step 2: Lcm  Lft x 30


Lft

 Step 3: Print Lcm Lcm  Lft x 30

Print
Lcm

STOP
Example 3
Write an algorithm and draw a flowchart that will read the two
sides of a rectangle and calculate its area.
Pseudocode
 Input the width (W) and Length (L) of a rectangle
 Calculate the area (A) by multiplying L with W
 Print A
Example 3
Algorithm START

 Step 1: Input W,L Input


W, L
 Step 2: A  L x W
 Step 3: Print A ALxW

Print
A

STOP
Flowcharts for three constructs
Pseudocode for three constructs
Example 1

Write an algorithm in pseudocode that finds


the average of two numbers

Solution
See Algorithm 8.1 on the next slide.
Algorithm 8.1: Average of two
AverageOfTwo
Input: Two numbers
1. Add the two numbers
2. Divide the result by 2
3. Return the result by step 2
End
Example 2

Write an algorithm to change a numeric


grade to a pass/no pass grade.

Solution
See Algorithm 8.2 on the next slide.
Algorithm 8.2: Pass/no pass Grade
Pass/NoPassGrade
Input: One number
1. if (the number is greater than or equal to 70)
then
1.1 Set the grade to “pass”
else
1.2 Set the grade to “nopass”
End if
2. Return the grade
End
Example 3

Write an algorithm to change a numeric


grade to a letter grade.

Solution
See Algorithm 8.3 on the next slide.
Algorithm 8.3: Letter grade
LetterGrade
Input: One number
1. if (the number is between 90 and 100, inclusive)
then
1.1 Set the grade to “A”
End if
2. if (the number is between 80 and 89, inclusive)
then
2.1 Set the grade to “B”
End if
Continues on the next slide
Algorithm 8.3: Letter grade (continued)
3. if (the number is between 70 and 79, inclusive)
then
3.1 Set the grade to “C”
End if
4. if (the number is between 60 and 69, inclusive)
then
4.1 Set the grade to “D”
End if

Continues on the next slide


Algorithm 8.3: Letter grade (continued)
5. If (the number is less than 60)
then
5.1 Set the grade to “F”
End if
6. Return the grade
End
Example 4

Write an algorithm to find the largest of a set


of numbers. You do not know the number of
numbers.
Solution
See Algorithm 8.4 on the next slide.
Algorithm 8.4: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 if (the integer is greater than Largest)
then
2.1.1 Set largest to the value of the integer
End if
End while
3. Return Largest
End
Example 5

Write an algorithm to find the largest of


1000 numbers.

Solution
See Algorithm 8.5 on the next slide.
Algorithm 8.5: Find largest of 1000 numbers
FindLargest
Input: 1000 positive integers
1. Set Largest to 0
2. Set Counter to 0
3. while (Counter less than 1000)
3.1 if (the integer is greater than Largest)
then
3.1.1 Set Largest to the value of the integer
End if
3.2 Increment Counter
End while
4. Return Largest
End
8.5

SUBALGORITHMS
Figure 8-9
Concept of a subalgorithm
Algorithm 8.6: Find largest
FindLargest
Input: A list of positive integers
1. Set Largest to 0
2. while (more integers)
2.1 FindLarger
End while
3. Return Largest
End
Subalgorithm: Find larger
FindLarger
Input: Largest and current integer
1. if (the integer is greater than Largest)
then
1.1 Set Largest to the value of the integer
End if
End
Thank You…!

You might also like