DS Intro Algorithms (1) SLM
DS Intro Algorithms (1) SLM
and
Algorithms
Program Development
2
Algorithm
How to
prepare
tea ??
Relate with
algorithm…
How to prepare Maggi
noodles ??
1. Simple English
2. Graphical representation like flow chart
3. Programming language like c/c++ / java
4. Combination of above methods.
Control Structures
Sequence
Decision Making
Looping
10
“Weekly Pay” Example
11
Step A: Pseudocode
Start with a plain English description, then…
1. Display “Number of hours worked: ”
2. Get the hours
3. Display “Amount paid per hour: ”
4. Get the rate
5. Compute pay = hours * rate
6. Display “The pay is $” , pay
12
Flowchart Symbols
Start
Start Symbol Input/Output
End
End Symbol Decision Symbol
13
Step B: Flowchart
Display “Number
pay = hours * rate
of hours worked: ”
Display “Amount
paid per hour: ” End
14
Sequence
One step after another, with no branches
15
Decision Making
Selecting one choice from many based
on a specific reason or condition
16
Decision Making: Pseudocode
Answer the question “Is a number positive?”
17
Decision Making: Flowchart
TRUE FALSE
num > 0
Display Display
“It is positive” “It is negative”
End
18
Looping
Doing something over and over again
19
Looping: Pseudocode
Write an algorithm that counts from 1-20
1. Set num = 1
2. While num <= 20
3. Display num
4. num = num + 1
5. (End loop)
20
Looping: Flowchart
Start num = 1
TRUE Display
num >= 20 num
num = num + 1
FALSE
End
21
Looping: Flowchart
Start num = 1
TRUE Display
num <= 20 num
num = num + 1
FALSE
End
22
Performance Analysis
• Space complexity
– Space Complexity of an algorithm is total space taken by
the algorithm with respect to the input size.
– Space complexity includes both Auxiliary space and space
used by input.
• Time complexity
Time Complexity
• Space complexity
• Time complexity