02 EKH Algorithm Flowchart Complexity Lecture
02 EKH Algorithm Flowchart Complexity Lecture
What is an Algorithm?
Simple Example
Types of Algorithms
Flowchart:
Example 2: Design an algorithm and flowchart to input fifty numbers and calculate their
sum.
Algorithm:
Step1: Start
Flowchart
Algorithm vs Flowchart
Purpose To describe the logic clearly To visualize the logic and flow
1. Simplifies Understanding
2. Improves Communication
3. Aids in Problem-Solving
4. Helps in Debugging
Real-World Examples
✔ Easy to understand
✔ Visualizes logic clearly
✔ Reduces errors
✔ Aids in debugging and documentation
✔ Improves collaboration and communication
Pseudocode: A Beginner’s Guide
What is Pseudocode?
Keyword Purpose
START
INPUT A
INPUT B
SET SUM ← A + B
OUTPUT SUM
END
START
INPUT number
IF number % 2 = 0 THEN
OUTPUT "Even"
ELSE
OUTPUT "Odd"
END IF
END
START
INPUT A, B, C
IF A > B AND A > C THEN
OUTPUT A
ELSE IF B > C THEN
OUTPUT B
ELSE
OUTPUT C
END IF
END
START
FOR i ← 1 TO 5 DO
OUTPUT i
END FOR
END
START
INPUT N
SET FACT ← 1
FOR i ← 1 TO N DO
SET FACT ← FACT * i
END FOR
OUTPUT FACT
END
Pseudocode vs Flowchart
Explaining to programmers
Teaching beginners
2. Time Complexity
It describes how the runtime of an algorithm increases with the size of the input, usually
represented by n.
We use Big O notation to describe the upper bound (worst-case) behavior.
O(1) Constant Accessing array element Same time regardless of input size
O(n) Linear Linear Search Time grows directly with input size
O(2ⁿ) Exponential Recursive Fibonacci Time doubles with each additional input
Examples
def get_first_element(arr):
return arr[0]
→ Always one step, regardless of array size.
O(n) – Linear Time
def find_max(arr):
max_val = arr[0]
for val in arr:
if val > max_val:
max_val = val
return max_val
→ Must check each item once → grows with n.
def bubble_sort(arr):
for I in range(len(arr)):
for j in range(len(arr) – I – 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
→ Each item compared with each other → n × n.
3. Space Complexity
It measures how much additional memory an algorithm needs as the input size increases.
Example
def sum_array(arr):
total = 0
for num in arr:
total += num
return total
→ Uses constant space: one variable (total) → O(1)
When Space Matters
• Best Case: Minimum time (e.g., searching and finding on the first try)
• Worst Case: Maximum time (used for Big O)
• Average Case: Expected time for random inputs
Binary Search Example:
Best O(1)
Worst O(log n)
Average O(log n)
5. Tradeoffs
Sometimes:
• Faster time needs more space (and vice versa)
• Choosing between algorithm efficiency and implementation simplicity