lecture 2 Algorithms
lecture 2 Algorithms
Algorithms
1
Today’s Agenda
• Algorithm
• Categories of Algorithms
• Parts of Algorithms
• Characteristics of Algorithms
• Algorithm Representation
• Flowcharts
• Pseudocode
• Performance of Algorithms
2
Algorithm
• What is Algorithm?
• A finite set of states having well defined steps for solving
a particular problem.
Dos Don'ts
ALG1: Sum of two Numbers ALG2: Increment a Variable
Step 1 START Step 1 START
Step 2 Read A Step 2 Set a=1
Step 3 Read B Step 3 while (1)
Step 4 Sum= A+B Step 4 a = a+1
Step 5 PRINT (SUM) Step 5 End Loop
Step 6 END Step 6 END
3
Algorithm
• It is not the complete program or code
• It is solution (logic) of a problem
• In DS it defines generic mathematical model for
implementation.
• It can be represented either as:
• Flowchart or
• Pseudo code.
4
Categories of Algorithm
• The major categories of algorithms are:
• Sort
• Search
• Delete
• Insert
• Update
5
Parts of Algorithm
• Specification: Description of the computational procedure.
• Pre-conditions: The condition(s) on input.
• Body of the Algorithm: A sequence of clear and
unambiguous instructions.
• Post-conditions: The condition(s) on output.
6
Class Activity
• Write an algorithm to print a number until user
presses N or n.
7
Characteristics of an
Algorithm
• An algorithm must follow the mentioned below
characteristics:
• Input: An algorithm must have 0 or well defined inputs.
• Output: An algorithm must have 1 or well defined outputs,
and should match with the desired output.
• Finiteness: An algorithm must be terminated after the finite
number of steps.
• Feasibility: Should be feasible with the available resources.
• Independent: An algorithm must have step-by-step
directions which is independent of any programming code.
• Unambiguous: An algorithm must be unambiguous and
clear. Each of their steps and input/outputs must be clear
and lead to only one meaning.
8
Algorithm Representation
• Writing code before fully understanding its
underlying algorithm can lead to bugs
• There are no well-defined standards for writing
algorithms.
• It is problem dependent
• Common constructs can be used to write an
algorithm.
• Two options are:
• flowcharts
• pseudocode
9
Using Flowcharts to
Represent Algorithms
• A flowchart is a visual representation of an
algorithm's control flow.
• This representation illustrates
• statements that need to be executed,
• decisions that need to be made,
• logic flow (for iteration and other purposes), and
• terminals that indicate start and end points.
10
Using Flowcharts to
Represent Algorithms
• Symbols used to visualize algorithms
11
Using Flowcharts to
Represent Algorithms
12
Using Pseudocode to
Represent Algorithms
• Another alternative is pseudocode
• It is a textual representation of an algorithm that
approximates the final source code.
• Syntax is not a concern
• There are no hard-and-fast rules for writing
pseudocode.
13
Using Pseudocode to
Represent Algorithms
• Consider the following pseudocode representation
of the previous counter-oriented flowchart:
ALG1: This Algorithm reads characters until a
newline (\n) character is entered.
START
DECLARE CHARACTER ch = ''
DECLARE INTEGER count = 0
DO
READ ch
IF ch GE '0' AND ch LE '9' THEN
count = count + 1
END IF
UNTIL ch EQ '\n'
PRINT count
14
END
Class Activity 2
• Design an algorithm to multiply two numbers x and y and
display the result in z.
Step 1 START
Step 2 declare three integers x, y & z
Step 3 define values of x & y
Step 4 multiply values of x & y
Step 5 store the output of step 4 in z
Step 6 print z
Step 7 STOP
16
Performance of Algorithms
• The data structures and algorithms we use can
critically affect two factors in our applications
17
Performance of Algorithms
• Memory Usage
• Space complexity: It is the amount of memory space
required by an algorithm, during a course of its
execution.
• CPU Time
• Time complexity: It is a way of representing the amount
of time needed by a program to run to the completion.
• Inverse Relationship?
18
Homework
• Design flowchart for Class Activity 1.
19