Unit II - Session 9

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

UNIT II

DIVIDE AND CONQUER

Session – 9
Syllabus
 Introduction, Binary Search - Merge sort and its
algorithm analysis - Quick sort and its algorithm
analysis - Strassen's Matrix multiplication - Finding
Maximum and minimum - Algorithm for finding closest
pair - Convex Hull Problem
Introduction
Divide/ Break
Conquer / Solve
Merge / Combine
BINARY SEARCH ALGORITHM
Binarysearch(a[n], key)
start=0; end=n-1;
While(start<=end)
{
mid=(start+end)/2;
return mid
if(a[mid]==key)
return mid;
else if(a[mid]<key)
end=mid-1;
else
start=mid+1;
}
return -1;
ANALYSIS
Using Master’s theorem
T(n) = T(n/2) + 1
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
a=1 b=2 f(n) = 1 = n0

1. If f(n) < O(nlogba), then T (n) = ϴ (nlogba).

2. If f(n) = ϴ (nlogba) , then T (n) = ϴ (nlogbalogn).

3. If f(n) > Ω (nlogba), and f(n) satisfies the regularity condition, then T
(n) = ϴ (f(n)).

Calculate nlogba = nlog21 = n0


Compare with f(n). Since f(n) < nlogba
i.e. n0 = n0
Case 3 is satisfied hence complexity is given as T(n) = Θ(f(n)) = Θ
(n0logn) = Θ (logn)
Worksheet No. 9

You might also like