Design and Analysis of Algorithms (CS3052)
Design and Analysis of Algorithms (CS3052)
Algorithms (CS3052)
Prof. G. G. Shingan
Computer science and Engineering
Department
RIT, Rajaramnagar.
Lecture No. 5
Content
• Performance Analysis of Binary Search
• Applications of Divide and Conquer: Merge Sort
• Performance Analysis of Merge Sort
Learning Outcomes
• Understand Divide and Conquer Strategy
• Analysis the complexity of Binary Search
• Design and Analysis the complexity of Merge Sort
Binary Search
• To search a list of n items, first look at the item in
location n/2
▫ Then search either the region from 0 to (n/2)-1
or the region from (n/2)+1 to (n-1)
• Example: searching for 57 in a sorted list of 15
numbers
③
②
①
Binary Search
Approximate number of comparisons (worst case):
search
http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
https://www.cs.usfca.edu/~galles/visualization/Search.html
https://blog.penjee.com/binary-vs-linear-search-animated-gifs/
BINARY SEARCH
Input: An array a[low….high] of elements sorted in non
decreasing order
Procedure T(n)
binarysearch(low,high)
1
if (low=high) then
1 { if x=A[i]then return 0 else return 1; }
else
mid (low high)/2
1
if x=A[mid] then return mid T(n/2)
1 else if x<A[mid] then return binarysearch(low,mid-1)
else return binarysearch(mid+1,high)
end if
T(n/2)
2-------------------- n=1
Recurrence relation T(n)
T(n/2)+2----------n>1
• T(n)=T(n/2)+2-------(I)
=T(n/4)+(2+2)
=T(n/4)+(2*2)
T(n/2)=T(n/4)+2----------(II)
=T(n/8)+(2+2+2)
=T(n/23)+(3*2) T(n/4)=T(n/8)+2----------(IV)
= T(n/16)+(2+2+2+2)
= T(n/24)+(4*2)
………….
= T(n/2k)+(k*2)
for simplicity assume that n=2k n=2k
T(n)= T(2k/2k)+(k*2) Apply log on both sides
Log n =k log 2
T(n)=T(1)+(k*2) K=log n/log 2
T(n)=2+2*log 2 n K=logn2
=O(log n)
Merge Sort
Merge Sort
• https://visualgo.net/bn/sorting
• https://www.hackerearth.com/practice/algorith
ms/sorting/merge-sort/visualize/
Merge sort Algorithm
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
9 6 5 Merge(1,2,3) 0 8 2 Merge(1,4,6)
MS(6,6)
9 6 Merge(1,1,2) 0 8 Merge(4,4,5)
MS(1,1) MS(2,2) MS(4,4) MS(5,5)
MS(1,6)
Example 1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)
1 2 3 4 5 6
9 6 5 0 8 2
MS(1,3) MS(4,6)
9 6 5 0 8 2 Merge (1,3,6)