Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
Experiment No. 4
Finding Maximum and Minimum
Date of Performance: 05/02/2025
Date of Submission: 12/02/2025
Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
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, thefollowing 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 orequal 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: 2024-25 (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.
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
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
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
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)
end
Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
Code:
#include <stdio.h>
int step = 1;
void DC_MAXMIN(int arr[], int low, int high, int *min, int *max) {
int mid, LMin, LMax, RMin, RMax;
printf("Step %d: low = %d, high = %d\n", step++, low, high);
if (low == high) {
*min = *max = arr[low];
printf("Base case: Single element %d -> min = %d, max = %d\n", arr[low], *min, *max);
return;
}
else if (low == high - 1) {
if (arr[low] < arr[high]) {
*min = arr[low];
*max = arr[high];
} else {
*min = arr[high];
*max = arr[low];
}
printf("Base case: Two elements (%d, %d) -> min = %d, max = %d\n", arr[low], arr[high],
*min, *max);
return;
}
mid = (low + high) / 2;
DC_MAXMIN(arr, low, mid, &LMin, &LMax);
DC_MAXMIN(arr, mid + 1, high, &RMin, &RMax);
*max = (LMax > RMax) ? LMax : RMax;
*min = (LMin < RMin) ? LMin : RMin;
printf("Merging: low = %d, mid = %d, high = %d -> LMin = %d, LMax = %d, RMin = %d,
RMax = %d -> min = %d, max = %d\n",
low, mid, high, LMin, LMax, RMin, RMax, *min, *max);
}
int main() {
int n;
printf("Enter the size of the array: ");
Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int min, max;
printf("\n--- Iteration Steps ---\n");
DC_MAXMIN(arr, 0, n - 1, &min, &max);
printf("\nFinal Result:\n");
printf("Minimum Element: %d\n", min);
printf("Maximum Element: %d\n", max);
return 0;
}
Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
Output:
Conclusion:
The Divide and Conquer Min-Max Algorithm efficiently finds the minimum and
maximum values in an array by recursively dividing it into smaller subarrays. This
approach reduces the number of comparisons compared to a simple linear scan,
making it more efficient for large datasets. With a time complexity of O(n) in the best
case and O(1) space complexity, it optimally balances performance and memory
usage.
Sem IV, Analysis of Algorithm Lab (CSL401) Department Of Computer Engineering