Introduction to Algorithms 1 Dr Khizar Hayat Associate
- Slides: 14
Introduction to Algorithms - 1 Dr. Khizar Hayat Associate Prof. of Computer Science
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
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
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
Operators Arithmatic Operators Operation Operator Add + Subtract - Multiply * Divide / ‘Modulo divide’ or mod % Logical Operators Operation Operator AND && OR || NOT !
Operators Relational Operators Operation Operator (C++) Equal to = == Greater than > > Less than < < Greater than or equal ≥ >= Less than or equal ≤ <= Not equal to ≠ !=
Key features of an Algorithm • Sequence/order/process • Decision/Selection • Repetition/Iteration/looping
Methods of specifying an algorithm 1. Flowchart 2. Pseudocode
Flowchart • A graphical way to express an algorithm – “A picture is worth a thousand words” • Symbols (ANSI) 2 - Process 1 - Terminal 3 - Input/output Start Stop 4 - Decision 5 - Connector count = 5? Yes No 6 - Predefined Process
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
Algorithm to add two numbers 1. begin 2. get num 1 3. get num 2 4. set sum num 1 + num 2 5. display sum 6. end Start Read Num 1 Read Num 2 Sequence Sum Num 1 + Num 2 Display Sum Stop
Algorithm to input two numbers and print the smaller number Start 1. begin 2. get num 1 3. get num 2 4. if num 1 < num 2 5. then display num 1 as smaller 6. else display num 2 as smaller 7. end if 8. end Selection Read Num 1 Read Num 2 Num 1 < Num 2? No Yes Display Num 1 as min Stop Display Num 2 as min
Algorithm to sum five numbers input by the user 1. begin 2. set count 0 3. set sum 0 4. repeat until count=5 5. get num 6. set count+1 7. set sum+num 8. end repeat 9. display sum as the total 10. end Repetition Start count 0 sum 0 Read Num count + 1 sum + Num count = 5? Yes Display sum as total Stop No
Algorithm to compute the GCD of two numbers Start Read Num. A Read Num. B Num. A > Num. B? No Yes Maqs Num. A Maqs Num. B M_Alaih Num. A Baqi Maqs mod M_Alaih Baqi = 0? No Yes Display M_Alaih as GCD Stop Maqs M_Alaih Maqs mod M_Alaih
- Hayat ve hayat dışı sigortalar arasındaki farklar
- An introduction to the analysis of algorithms
- Bioinformatics
- Design and analysis of algorithms introduction
- Introduction to algorithms
- Introduction to algorithms slides
- Introduction to algorithms 2nd ed
- Introduction to algorithms
- Introduction to algorithms lecture notes
- Introduction to the design and analysis of algorithms
- Introduction to sorting algorithms
- Introduction to algorithms 2nd edition
- Introduction to algorithms 2nd edition
- Introduction to bioinformatics algorithms
- Bioalgorithms