ALGO MS Week 1
ALGO MS Week 1
(AAA)
Dr. Shahzad Sarwar Bhatti
Assistant Professor,
Department of Information Sciences
Complete
Finite
At least one Output
0,1 or more inputs
Correct
Clarity
Characteristics of Algorithm
The main x-tics of algorithms are as follows:
• Algorithms must have a unique name.
• Algorithms should have explicitly defined set of inputs and
outputs.
• Algorithms are well-ordered with unambiguous operations.
• Algorithms halt in a finite amount of time. Algorithms should not
run for infinity, i.e., an algorithm must end at some point.
Characteristics of Algorithm (cont’d)
What is Pseudo code?
Comments
– Each step start with a comment
– Enclose in [ ]
– Define the purpose of step
Input Statements
– Read
– Scanf (if using C/C++)
Output statement
– Write
– Printf (if using C/C++)
Algorithms Notations (cont’d)
Selection statement
– If –Then –End If
– If – then ---Else --- End If
– Nested If
– Example
If ( a>b ) then
write ( a+”Is Large”)
Else
write ( b+”Is Large”)
End if
Algorithms Notations (cont’d)
Loops ( For, While, Until )
– Example-1
Repeat Step 2 For i=1, N=10
– Example-2
Repeat Step 2 to Step 4 For i=1, N, 10
– Example-3
Repeat Step 2 while i<=10
– Example-4
Repeat Step 2 Until i>10
Algorithms Notations (cont’d)
Finish
– Exit (Used in main algorithm)
– Return (Used in sub algorithm)
Example - 1
Write an algorithms or Pseudo code to read two number
and display the largest number.
Algorithm Example - 1
Algorithm LARGE(No1, No2, lar)
{ This algorithm is used to read two numbers and print the largest}
Step-1 [ Read numbers No1 and No2]
Read(No1,No2)
Step-2 [Find the largest]
if (No1 > No2) then
lar = No1
else
lar = No2
end if
Algorithm Example – 1 (cont’d)
Step-3 [Display the Result ]
Write(lar)
Step-4 {Finish]
Exit
Algorithm Example – 1 (cont’d)
Pseudo Code Example – 1 (cont’d)