0% found this document useful (0 votes)
8 views

SE_Sem_IV_AoA_Lab_Experiment_4_202425

The document outlines Experiment No. 4 for a computer engineering lab, focusing on finding the maximum and minimum values in an array using both naive and divide-and-conquer methods. It details the algorithms, their time complexities, and provides a recursive approach to solving the problem. The experiment aims to introduce students to greedy algorithms and enhance their understanding of algorithm analysis.

Uploaded by

Jagruti Chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SE_Sem_IV_AoA_Lab_Experiment_4_202425

The document outlines Experiment No. 4 for a computer engineering lab, focusing on finding the maximum and minimum values in an array using both naive and divide-and-conquer methods. It details the algorithms, their time complexities, and provides a recursive approach to solving the problem. The experiment aims to introduce students to greedy algorithms and enhance their understanding of algorithm analysis.

Uploaded by

Jagruti Chavan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment No.

4
Finding Maximum and Minimum
Date of Performance:
Date of Submission:

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Experiment No. 4

Title: Finding Maximum and Minimum

Aim: To study, implement, analyze Finding Maximum and Minimum Algorithm


using Greedy method

Objective: To introduce Greedy based algorithms

Theory:

Maximum and Minimum can be found using a simple naïve method.

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 in Naive method is 2n - 2.

The number of comparisons can be reduced using the divide and conquer approach.
Following is the technique.

2. Divide and Conquer Approach:

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].

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2023-24 (Even Sem)

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)

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


Algorithm:
DC_MAXMIN (A, low, high)
// Input: Array A of length n, and indices low = 0 and high = n-1
// Output: (min, max) variables holds minimum and maximum
if low = = high, Then // low = = high
return (A[low], A[low])
else if low = = high - 1 then //low = = high - 1
if A[low] < A[high] then
return (A[low], A[high])
else
return (A[high], A[low])
else
mid ← (low + high) / 2
[LMin, LMax] = DC_MAXMIN (A, low, mid)
[RMin, RMax] = DC_MAXMIN (A, mid + 1, high)
// Combine solution
if LMax > RMax, Then
max ← LMax
else
max ← RMax
end
if LMin < RMin, Then // Combine solution.
min ← LMin
else
min ← RMin
end
return (min, max)

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering


end
Code:

Output:

Conclusion:

Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering

You might also like