AOA Experiment 4
AOA Experiment 4
Experiment No. 4
Finding Maximum and Minimum
Date of Performance: 05/02/2025
Date of Submission: 12/02/2025
Experiment No. 4
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, thefollowing 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 orequal 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.
Vidyavardhini’s College of Engineering and Technology
Department of Computer Engineering
Academic Year: 2024-25 (Even Sem)
Time Complexity:
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
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;
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;
}
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: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("\nFinal Result:\n");
printf("Minimum Element: %d\n", min);
printf("Maximum Element: %d\n", max);
return 0;
}
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.