Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Algorithms - 1

Similar presentations


Presentation on theme: "Introduction to Algorithms - 1"— Presentation transcript:

1 Introduction to Algorithms - 1
Dr. Khizar Hayat Associate Prof. of Computer Science

2 Algorithm A sequence of unambiguous instructions to solve a problem
A well-defined procedure that takes some value(s), process it and produce some value(s), as output Hence algorithm has three parts: Input Processing or transformation output

3 Properties of an algorithm
Finiteness Terminates in finite number of steps Definiteness Each step must be clearly and carefully specified Input Valid inputs clearly specified Output Can be proved to produce the correct output given a valid input Effectiveness Steps must be simple and atomic

4 Variable A named location that stores a value
A place holder for a data The data may change from time to time during execution: only the last change is reflected in a given variable Named by the programmer Assigning value to a variable means putting/storing some value in the location corresponding to the variable A5 or A:=5 A=5 in C++ and JAVA

5 Operators Arithmatic Operators Logical Operators Operation Operator
Add + Subtract - Multiply * Divide / ‘Modulo divide’ or mod % Logical Operators Operation Operator AND && OR || NOT !

6 Operators Relational Operators Operation Operator Operator (C++)
Equal to = == Greater than > Less than < Greater than or equal >= Less than or equal <= Not equal to !=

7 Key features of an Algorithm
Sequence/order/process Decision/Selection Repetition/Iteration/looping

8 Methods of specifying an algorithm
Flowchart Pseudocode

9 Flowchart A graphical way to express an algorithm Symbols (ANSI)
“A picture is worth a thousand words” Symbols (ANSI) 1- Terminal 2- Process 3- Input/output Start Stop 4- Decision 6- Predefined Process 5- Connector count = 5? No Yes

10 Pseudocode Pseudo means fake or mock or dummy زائف
It is not proper C++ or Java code but a mock code understandable to any programmer Language independence: Must not use constructs/keywords from C++/Java or any other programming language Allows the designer to focus on logic rather than language syntax Use proper indentation Always end your constructs! Must Begin and END code/loops/ if

11 Algorithm to add two numbers
begin get num1 get num2 set sum  num1 + num2 display sum end Start Read Num1 Read Num2 Sum Num1 + Num2 Sequence Display Sum Stop

12 Algorithm to input two numbers and print the smaller number
Start begin get num1 get num2 if num1 < num2 then display num1 as smaller else display num2 as smaller end if end Read Num1 Read Num2 Num1 < Num2? No Selection Yes Display Num1 as min Display Num2 as min Stop

13 Repetition Algorithm to sum five numbers input by the user begin
Start count 0 begin set count0 set sum0 repeat until count=5 get num set countcount+1 set sumsum+num end repeat display sum as the total end sum 0 Read Num count count + 1 sum sum + Num count = 5? No Repetition Yes Display sum as total Stop

14 Algorithm to compute the GCD of two numbers
Display M_Alaih as GCD Stop Maqs NumA M_Alaih NumB Start Read NumA Read NumB NumA > NumB? Yes No Maqs NumB M_Alaih NumA Baqi = 0? Maqs M_Alaih M_Alaih Maqs mod M_Alaih Baqi Maqs mod M_Alaih


Download ppt "Introduction to Algorithms - 1"

Similar presentations


Ads by Google