Design and Analysis of Algorithm Lecturer Shahid Imran





![Algorithm Notations • Comments • Each step start with comments • Enclosed in [] Algorithm Notations • Comments • Each step start with comments • Enclosed in []](https://slidetodoc.com/presentation_image_h2/0fccede2802d1dfc8d23cc68ad7371e0/image-6.jpg)







![Algorithm For Example 2 Algorithm SUM (Ary[], I, total, N) {This algorithm is used Algorithm For Example 2 Algorithm SUM (Ary[], I, total, N) {This algorithm is used](https://slidetodoc.com/presentation_image_h2/0fccede2802d1dfc8d23cc68ad7371e0/image-14.jpg)
![Algorithm For Example 2 Step-5 [Add the values] total = total + Ary[i] Step-6 Algorithm For Example 2 Step-5 [Add the values] total = total + Ary[i] Step-6](https://slidetodoc.com/presentation_image_h2/0fccede2802d1dfc8d23cc68ad7371e0/image-15.jpg)


- Slides: 17

Design and Analysis of Algorithm Lecturer: Shahid Imran

What is an Algorithm? • An Algorithm is a step by step procedure to solve a problem • Plate from independent • Can be implemented in any procedural language • Simples Form is Pseudo Code • Pseudo code can be written in any language • Two Important Factors • Space tradeoff (Refer to less use of memory RAM) • Time tradeoff (Refer to less time of micro processor for execution)

Features of Algorithm • Complete • Finite • At least one output • 0, 1 or more inputs • Correct • Clarity

Algorithm Example Algorithm SUM (No 1, No 2, Result) {This algorithm is used to read two numbers and print sum of both} Step-1 [Read No 1, No 2] Read(No 1, No 2) Step-2 [Calculate the sum] Result= No 1 + No 2 Setp-3 [Display the result] Write(Result) Step-4 [Finish] Exit C Language Program void main() { int a, b, result; scanf(“%d%d”, &a, &b); result=a+b; printf(“Sum =%d”, result); }

Algorithm Notations • Algorithm Name • • Should be in capital Meaningful • Parameters • • • Should be enclosed in parenthesis () Variable name comprises more than one character Scripting or looping variables are of single character • Introductory Comments • Descriptions and purpose of the algorithm • Steps • Finite
![Algorithm Notations Comments Each step start with comments Enclosed in Algorithm Notations • Comments • Each step start with comments • Enclosed in []](https://slidetodoc.com/presentation_image_h2/0fccede2802d1dfc8d23cc68ad7371e0/image-6.jpg)
Algorithm Notations • Comments • Each step start with comments • Enclosed in [] • Define the purpose of step • Input statement • Read / scanf • Output Statement • Write / printf

Algorithm Notations • Selection Statement • If – then – endif • If – then --- else --- endif • Nested if • Example If (a>b) then Write (a+”is large”) Else write (b+”is large”) End if

Algorithm Notations • Loops (For, While, Until) • Example 1 • Repeat step 2 for i=1, N, 1 • Example 2 • Repeat step 2 to 4 for i=1, N, 1 • Example 3 • Repeat step 2 while i<=10 • Example 4 • Repeat step 2 until i>10

Algorithm Notations • Finish • Exit • • Used in main algorithm Return • Used in sub algorithm

Example 1 • Write an algorithm or pseudo code to read two numbers and display the largest number

Algorithm For Example 1 Algorithm LARGE (No 1, No 2, lar) {This algorithm is used to read two numbers and print the largest} Step-1 [Read No 1 and No 2] Read(No 1, No 2) Step-2 [Find the largest] if (No 1>No 2) then lar=No 1 else lar=No 2 endif Step-3 [Display the result] Write(lar) Step-4 [Finish] Exit

Algorithm Conventions in Example 1 • Algorithm Name (Large) • Parameters(No 1, No 2, lar) • Input and output statements (Read, Write) • Selection Statement (if-else) • Comments (enclosed in [ ] before start of each step) • Introductory comments (enclosed in {}) • Finish (exit)

Example 2 • Write an algorithm or pseudo code to read an array of N numbers and calculate its sum and print the sum
![Algorithm For Example 2 Algorithm SUM Ary I total N This algorithm is used Algorithm For Example 2 Algorithm SUM (Ary[], I, total, N) {This algorithm is used](https://slidetodoc.com/presentation_image_h2/0fccede2802d1dfc8d23cc68ad7371e0/image-14.jpg)
Algorithm For Example 2 Algorithm SUM (Ary[], I, total, N) {This algorithm is used to read an Array Ary of size N and display its sum } Step-1 [Perform a loop to read the values of array] Repeat Step 2 for i = 1, N, 1 Step-2 [Read Values] Read (Ary[i]) Step-3 [initialize the variable i and total] a. i=1 b. total =0 Step-4 [Perform a loop to traverse the all elements of array Ary and add] Repeat step 5 while i<=10
![Algorithm For Example 2 Step5 Add the values total total Aryi Step6 Algorithm For Example 2 Step-5 [Add the values] total = total + Ary[i] Step-6](https://slidetodoc.com/presentation_image_h2/0fccede2802d1dfc8d23cc68ad7371e0/image-15.jpg)
Algorithm For Example 2 Step-5 [Add the values] total = total + Ary[i] Step-6 [Display the sum] Write (total) Step-7 [Finish] exit

Example 3 • Write an algorithm or pseudo code to find the factorial of number N using sub algorithm approach

Algorithm For Example 3 Algorithm FACTORIAL (No, Res) {This algorithm is used to read a number and print its factorial} Step-1 [Read the number] Read (No) Step-2 [Call the sub algorithm FACT] Call Res = FACT(No) Step-3 [Display the Factorial] Write (Res) Step-4 [Finish] Exit Sub. Algorithm FACT(New. No, i, f) { This sub algorithm is used to receive an element from main algorithm FACTORIAL and return the result } Step-1 [Intialize the variable] f=1 Step-2 [Perform loop to calculate factorial] Repeat step 3 for i=1, New. No, 1 Step-3 [Calculate] f=f*I Step 4 [Finish] return(f)