Algorithm Properties
- Input
(zero or more) - Output
(at least one) - Correct
- Language Independent
- Unambiguous
- Efficiency
- Time Complexity: Amount of time taken
- Space Complexity: Amount of memory used
Time Complexity Analysis
Example 1: Sum of Two Numbers
(1) Read a, b → Assignment Statement (2 time units)
(2) S = a + b → Constant time 'c' (2 time units)
Logic Operation
(3) Print S → Print operation (1 time unit)
Total Time:
Example 2: Sum of n Numbers
S = 0 → 1 time unit
for i = 0 to n → 2n time units
S = S + A[i] → 2n time unitsAnalysis:
numbers input operations - Loop executes
times - Each iteration: constant time operations
Example 3: Matrix Addition - 2D Array
for i = 0 to n → Outer loop runs n times
for j = 0 to m → Inner loop runs m times
C[i,j] = A[i,j] + B[i,j] → Executed n × m timesTime Complexity:
Note: For square matrices where
Fibonacci Algorithm
Iterative Algorithm: Time Complexity
Fib(n)
f1 = 0
f2 = 1
for i = 2 to n
temp = f1 + f2
f1 = f2
f2 = temp
return f2Recursive Definition: