SE_Sem_IV_AoA_Lab_Experiment_4_202425
SE_Sem_IV_AoA_Lab_Experiment_4_202425
4
Finding Maximum and Minimum
Date of Performance:
Date of Submission:
Theory:
1. Naïve Method:
Naïve method is a basic method to solve any problem. In this method, the maximum
and minimum number can be found separately. To find the maximum and minimum
numbers, the following straightforward algorithm can be used.
The number of comparisons can be reduced using the divide and conquer approach.
Following is the technique.
In this approach, the array is divided into two halves. Then using recursive approach
maximum and minimum numbers in each halves are found. Later, return the
maximum of two maxima of each half and the minimum of two minima of each half.
In this given problem, the number of elements in an array is y−x+1, where y is greater
than or equal to x.
DC_MAXMIN (A, low, high) will return the maximum and minimum values of an array
numbers[x...y].
Example:
Min= 11
Max=66
Min= 11
Min= 22
Max=44
Max=66
Logic used:
The given list has more than two elements, so the algorithm divides the
array from the middle and creates two subproblems.
Both subproblems are treated as an independent problem and the same
recursive process is applied to them.
This division continues until subproblem size becomes one or two.
If a1 is the only element in the array, a1 is the maximum and minimum.
If the array contains only two elements a1 and a2, then the single comparison
between two elements can decide the minimum and maximum of them.
Time Complexity:
The recurrence is for min-Max algorithm is:
T(n) = 0, if n = 1
T(n) = 1, if n = 2
T(n) = 2T(n/2) + 2, if n > 2
T(n) = 2T(n/2) + 2 … (1)
Substituting n by (n / 2) in Equation (1)
T(n/2) = 2T(n/4) + 2
= T(n) = 2(2T(n/4) + 2) + 2
= 4T(n/4) + 4 + 2 … (2)
By substituting n by n/4 in Equation (1),
T(n/4) = 2T(n/8) + 2
Substitute it in Equation (1),
T(n) = 4[2T(n/8) + 2] + 4 + 2
= 8T(n/8) + 8 + 4 + 2
= 23 T(n/23) + 23 + 22 + 21
……
T(n)= 2k T(n/2k) + 2k +…..+ 22 + 21
Assume n/2k=2 so n/2 = 2k
T(n) = 2k T(2) + ( 2k +…..+ 22 + 21)
T(n) = 2k T(2) + ( 21 + 22 +…..+ 2k )
Using GP formula : GP = a(rk – 1)/ (r-1)
Here a = 2 and r = 2
= 2k + 2(2k-1)/(2-1)
= 2k + 2k+1-2 { Assume n/2k=2 so n/2 = 2k and n/2k+1 =2(n/2)=n }
= n/2 + n – 2
= 1.5 n – 2
Time Complexity = O(n)
Output:
Conclusion: