Min- Max Algorithm
Min- Max Algorithm
The Divide and Conquer approach is a powerful algorithmic technique that can be
used to find the minimum and maximum values in an array efficiently.
This method involves breaking down the problem into smaller subproblems,
solving each subproblem independently, and then combining the solutions to get
the final result.
Algorithm Explanation
Conquer: Recursively find the minimum and maximum values in each half.
Combine: Compare the results from the two halves to get the overall minimum
and maximum values.
Base Cases
If the array contains only one element, that element is both the minimum
and maximum.
Recursive Case
Our problem is to find the minimum element and the maximum element from the
given array.
Let us see that how we will find minimum and maximum element without using the
divide and conquer method first.
Let us assume that we have an array of 5 elements which are not in a sorted order.
So, finally if we want to find minimum and maximum in the same algorithm it requires
---> min & max ---(n-1) + (n-1)
= 2n-2 comparisons.
So, the time complexity is O(n) after avoiding the constants.
So, in the absence of divide and conquer we require (2n-2) comparisons for
conventional model or linear approach.
Now let us see how many comparisons are required to solve such kind of same
problem using divide and conquer.
So, we will concentrate on the time complexity in terms of comparison using divide
and conquer method.
Example:
40 60 50 30 20 10
Here we have 6 elements and now we need to divide it into sub problems .So, for division here we are
taking the formula as “middle element” i.e mid value. Along with division we also need to know the STOP
CONDITION i.e (at which position we need to stop the recursion/division)
Low high
Elements 40 60 50 30 20 10
Index 0 1 2 3 4 5
So we need to divide the complete problem into 2 sub arrays (complete array into 2 sub arrays).
So, the 1st array will be from 0 to 2 (i.e. low to mid)
2nd array will be from 3 to 5 (i.e. mid+1 to high)
low high low high
40 60 50 30 20 10
0 1 2 0 1 2
mid=(0+2) / 2 mid=(0+2) / 2
mid = 1 mid = 1
So, we need to STOP the division of arrays now, because we need at least 2 elements for comparison. The STOP
k
In the remaining arrays i.e
30 20 10
Among 30 & 20 min = 20 and max = 30 , similarly 10 itself is max and min i.e min=10 and max = 10.
Since we solved the sub problems of the array until we reach the STOP condition. Now we need to
compare and combine .
40 60 50 30 20 10
Here Here
MIN = 40 AND MAX = 60 MIN = 10 AND MAX = 30
So now by comparing all the elements we obtain the solution as min = 10 and max = 60.
FINDING MINIMUM AND MAXIMUM ELEMENT USING DIVIDE AND CONQUER
ALGORITHM
MIN_MAX (low,high,min,max)
{
if ( low== high) //WHEN THE ELEMENTS ARE 1 , i.e N=1 element
{
min= a[low];
max=a[low];
}
else if (low== high – 1) //WHEN THE ELEMENTS ARE 2 , i.e N= 2 elements
{
if (a[low] < a[high])
{
min= a[low] ;
max=a[high];
}
else
{
min= a[high] ;
max=a[low];
}
}
else // WHEN THE ELEMENTS ARE GREATER THAN 2 i.e , N>2 elements
{
mid= low + high / 2
min_max (low, mid ,min1,max1) // FIRST ARRAY
min_max ( mid+1 , high ,min2,max2) // SECOND ARRAY
n=2k+1
TIME COMPLEXITY OR COMPARISONS
T(n) = 0 ----- if n = 1
= 1 ----- if n = 2
= 2T(n/2) + 2 -------- if n>2
Now for
----
----
K k-1
= 2 k T (n/2 ) + 2 + 2 +………+2
k
---- ----- ------ ------ ------- ------ ------
From the above obtained derivation i.e,
2k=n/2