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

Min- Max Algorithm

The document explains the Divide and Conquer algorithm for efficiently finding the minimum and maximum values in an array by recursively splitting the array into halves and combining the results. It outlines the base cases for arrays with one or two elements, and provides a detailed example demonstrating the process. The time complexity for this approach is O(n), which is more efficient than the conventional method requiring 2n-2 comparisons.

Uploaded by

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

Min- Max Algorithm

The document explains the Divide and Conquer algorithm for efficiently finding the minimum and maximum values in an array by recursively splitting the array into halves and combining the results. It outlines the base cases for arrays with one or two elements, and provides a detailed example demonstrating the process. The time complexity for this approach is O(n), which is more efficient than the conventional method requiring 2n-2 comparisons.

Uploaded by

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

Finding Min and Max Using Divide and Conquer

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

Divide: Split the array into two halves.

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.

 If the array contains two elements, compare them to determine the


minimum and maximum.

Recursive Case

 Divide the array into two halves.

 Recursively find the minimum and maximum values in each half.


Let us consider an array to find the minimum and maximum element using the
divide and conquer method.

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.

In order to find the minimum element , how many comparisons we require---


So, we need to compare the first element with all the other elements i.e, min 4
comparisons.
Similarly ,in order to find max 4 comparisons (is required).
If array is having “n” elements then we need (n-1) comparisons to find minimum
element and in the same way we need (n-1) comparisons to find maximum element.

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:

Let us consider few unsorted elements

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

mid= (low + high)/2


= (0 + 5) /2
= 2

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= (low + high) /2 mid= (low +


high) /2

mid=(0+2) / 2 mid=(0+2) / 2
mid = 1 mid = 1

So, by applying the same procedure we are dividing the arrays

low to mid mid+1 to high low to mid mid + 1 to


high 40 60 50 30 20 10

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

MIN = 40 MIN = 50 MIN = 20 MIN = 10


MAX = 60 MAX = 50 MAX = 30 MAX = 10

Here Here
MIN = 40 AND MAX = 60 MIN = 10 AND MAX = 30

Finally, MIN = 10 and MAX = 60.

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

If (min1 < min 2)


min = min 1;
else
min = min 2;
If (max1 > max 2) This part will be repeated until N/2k = 2
max = max 1;
else
max = max2;
}
}
k k
THE ABOVE PART WILL BE REPEATED UNTIL n/2 = 2( stop condition). This can further written as n=2 * 2

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

T(n/2)= 2[2T(n/2) /2 + 2]+2 = 4T(n/4) + 4 + 2

Again further division

T(n/2)= 4[2T(n/4) /2 +2] +4+ 2 = 8T(n/8) +8+ 4 + 2

Again further division

T(n/2)= 8[2T(n/8) /2 + 2] +8+4+ 2 = 16T(n/16) +16+8+ 4 + 2

----
----
K k-1
= 2 k T (n/2 ) + 2 + 2 +………+2
k
---- ----- ------ ------ ------- ------ ------
From the above obtained derivation i.e,

If the series is in the form 2+22+23+………2n.

Then Geometric Progression formula is a(rn – 1) / r-1


where “a” is the first term and “r” is the second term/ first term.

Geometric Progression = a(rn – 1) / r-1 (a is “2” and r is 4/2 =2 and n=k)


= 2(2k – 1) / 2-1

By considering the obtained solution from the Geometric Progression.

The above derivation can be written as


since n=2k* 2
n=2k+1

2k=n/2

So here also the time complexity is O(n).

For linear approach i.e (2n-2)

So if we consider n=10 elements for linear approach


we get 2(10)-2 comparisons i.e, 18 comparisons.

For Divide and Conquer approach 3n/2 -2

i.e 3(10)/2 – 2 = 13 comparisons , which was


reduced.

You might also like