Case Study (Analysis of Algorithm
Case Study (Analysis of Algorithm
Algorithm Analysis
Case Study
Algorithm Analysis
This case study is meant to demonstrate the salient features of algorithm design and
analysis, as discussed previously. A simple example is used to systematically describe the
following steps.:
• Problem statement
• Algorithm design
• Space complexity
• Correctness of algorithm
• Visualization of Analysis
Case Study
Problem Statement
¾ Design an algorithm to find maximum element in an array of size n
• Time efficiency
• Space efficiency
• Correctness
Case Study
Algorithm Design
The design features are expressed in plain language. The algorithm for the solution of
the problem consists of the following steps:
Step #3: Replace max with a larger element, when found during the scan
FIND-MAX ( A, n )
Best Case: Best case occurs when the statement is not executed at all. This happens when
the array maximum element occurs in the first cell. In this case k = 0, and best (minimum ) running time
is given by
Average Case: In this case, the statement is executed on an average n / 2 times so that k = n / 2. Thus,
average time running time is given by
Taverage(n) = A + (B / 2 + C).n
Worst Case: In this case, the statement is executed n-1 times; so k = n-1. This happens
when the array is sorted in ascending order .Thus, worst (maximum) running time is given by
FIND-MAX(A)
1 max ← A[1]
2 for j←2 to n do
3 if( A[j] > max)
4 then max ← A[j]
1 return max
Variable max holds the largest value at all stages of loop execution
• The Maintenance condition requires that if S is true before an iteration of loop, it should
remain true after the iteration It can be easily verified that if max holds the largest of k
elements, after kth iteration, then it holds largest of k+1 elements after the next iteration.
ie.(k+1)st iteration
• The Termination condition requires that post-condition should be true for problem size
i.e, max should return maximum array element. The loop terminates when index j exceeds
n. This implies that just after the last iteration max holds the largest of the first n elements
of the array. Since array has size n, it means that max returns the largest of array elements.
Analysis of Algorithm
Space Efficiency
The space analysis of algorithm for finding maximum element is simple and
straightforward. It amounts to determining space utilization as function of data structure size.
• The total space requirement consists of memory used by the program statements
and array element. The former is a fixed and does not depend on array size.
• The amount of storage requirement for the array depends on the nature of data
type (integer, floating point, strings). It increases in direct proportion to the array size
S(n) = A + B.n
Visualization
Visualization