0% found this document useful (0 votes)
119 views11 pages

Cauvery Institute of Technology, Mandya: Divide and Conquer

This document discusses algorithms for finding the maximum and minimum values in an array. It presents both an iterative and recursive approach. The iterative approach compares each element to the running maximum and minimum values, resulting in a time complexity of O(n). The recursive approach divides the array in half at each step to solve subproblems, resulting in a time complexity of O(n) as well when analyzed using the Master's theorem or substitution method.

Uploaded by

Hema Chander N
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)
119 views11 pages

Cauvery Institute of Technology, Mandya: Divide and Conquer

This document discusses algorithms for finding the maximum and minimum values in an array. It presents both an iterative and recursive approach. The iterative approach compares each element to the running maximum and minimum values, resulting in a time complexity of O(n). The recursive approach divides the array in half at each step to solve subproblems, resulting in a time complexity of O(n) as well when analyzed using the Master's theorem or substitution method.

Uploaded by

Hema Chander N
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/ 11

Cauvery Institute of

Technology, Mandya
Divide and Conquer
Minimum and Maximum Algorithm

Prof. Hemachander N,
Department of Computer Science &
Engineering,
CIT,Mandya
CIT,CSE
Recap
Boundary conditions of array
Binary search algorithm
Using divide and conquer [recursive approach]
Using Iterative procedure
Time Complexity- Best Case, Worst case and
average case

CIT,CSE
Finding the Maximum and Minimum

Problem Statement

The Max-Min Problem in algorithm


analysis is finding the maximum and
minimum value in an array.

CIT,CSE
Iterative Max_Min algorithm
Algorithm Straight_Max_Min(a[],n)
Purpose: Findig maximum and minimum element using Iterative procedure
Input: a- 1D array having n elements
n- size f the array a
Output: display max- maximum element, min- minimum element
Method:
max=min= a[0]
for i1 to n-1 do
if(a[i]>max) then
max=a[i]
end if
if(a[i]<min) then
min=a[i]
end if
end for
print max, min
END
CIT,CSE
Analyzing the Straight Forward Method

• In analyzing the time complexity of this algorithm, we have to


concentrate on the number of element comparisons. This
algorithm requires 2(n-1) element comparisons in the best,
average, and worst cases. An immediate improvement is
possible by realizing that the comparison a[i] < min is
necessary only when a[i]>max is false.
• Now the Best case occurs when the elements are in
increasing order. The number of element comparisons is n-1.
The worst case occurs when the element are in decreasing
order. In this case number of comparisons is 2(n-1).

CIT,CSE
Max_Min algorithm using Recursion
Algorithm MaxMin(i, j, max, min)
Purpose: is to set max and min to the largest and smallest values in a[i:j].
Input: a[1:n] is a global array. Parameters i and j are integers, // 1≤i≤j≤n.
i= beginning index
J= ending index
Output: max- largest element in a
min- smallest element in a
Method:
if (i=j) then max min  a[i]; //Small(P)
else if (i=j-1) then // Another case of Small(P)
{
if (a[i] < a[j]) then
max  a[j]; min  a[i];
else max a[i]; min a[j];
}
else
{
// if P is not small, divide P into sub-problems.
// Find where to split the set.
mid  ( i + j )/2;
// Solve the sub-problems.
MaxMin( i, mid, max, min );
MaxMin( mid+1, j, max1, min1 );
// Combine the solutions.
if (max < max1) then max  max1;
if (min > min1) then min  min1;
}
END
CIT,CSE
Trace
Elements: [10,20,30,40,50,60,70,80,90,100]
max: 100
min: 10

CIT,CSE
Time Recurrence relation
• T(n) represents the time complexity of
Max_Min algorithm and it is represented as
follws:

CIT,CSE
Time Complexity using Masters
Theorem
• T(n)= 2 T(n/2) +2
• Here a=2, b=2, f(n)=2
• Find d? i.e., d=nd 2* n0, Hence d=0
• a>bd ,i.e., 2>20, 2>1
• Thus for e.g. a=2, b=2 & d=0; hence, since a>bd
• Master theorm states that,
• T(n)€ θ(nlogb a) = θ(nlog2 2) = θ(n)

CIT,CSE
Time Complexity
using
Substitution
Method

T(n) represents recurrence


relations
T(n)=0 when n=1
T(n)=1 when n=2
T(n/2)+T(n/2)+2 when n>2

CIT,CSE
Next Video
• Merge Sort

CIT,CSE

You might also like