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

Design and Analysis of Algorithms (CS3052)

The document discusses performance analysis of binary search and merge sort algorithms. It provides examples and step-by-step workings of both algorithms. For binary search, it shows that the worst case running time is O(log n). For merge sort, it illustrates the divide and conquer approach and shows that the running time is O(n log n).
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Design and Analysis of Algorithms (CS3052)

The document discusses performance analysis of binary search and merge sort algorithms. It provides examples and step-by-step workings of both algorithms. For binary search, it shows that the worst case running time is O(log n). For merge sort, it illustrates the divide and conquer approach and shows that the running time is O(n log n).
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Design and Analysis of

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

start in the middle




Binary Search
Approximate number of comparisons (worst case):
search

N= 100 N = 1,000 N=10,000 N=1,00,000

search 100 1,000 10,000 1,00,000

bsearch 7 10 13.2877 16.60

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

Input: An array A[1..n] of n elements.


Output: A[1..n] sorted in nondecreasing order.
Procedure mergesort(low,high)
If (low=high) T(n)
{ // small(P) list is already sorted }
else
if (low<high) then T(n/2)
mid  (low  high)/2 
mergesort (low,mid) T(n/2)
mergesort (mid+1,high)
MERGE(low,mid,high)
end if O(n)
Merge Algorithm
MERGE(low,mid,high) If(h>mid) then
//A[low:high] is global array For k=j to high do
containing two sorted subsets in
A[low:mid] and A[mid+1:high] {b[i]=a[k]; i=i+1;}
//Merge two sets into single sets Else
residinf in A[low:high] {b[i]=a[k]; i=i+1;}
//B[] is Auxiliary global array
For k=low to high do
{ h=low; i=low; j=mid+1; A[k]=b[k];
while((h<=mid) and (j<=high))do }
{
if(a[h]<a[j]) then
{b[i]=a[h]; h=h+1;}
else
{b[i]=a[j]; j=j+1;}
i=i+1;
}
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)

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)

MS(1,2) MS(3,3) MS(4,5)


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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example1 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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Example MS(1,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)

MS(1,2) MS(3,3) MS(4,5)


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)
Space Complexity
• For size n
• levels=flower(log n)+1 i.e. depth of stack
• Size of stack=(log n+1)*k
=O(log n)
• Merging space= input size O(n)
• Total Space complexity= O(n+log n) =O(n)
Quick Sort
• https://www.hackerearth.com/practice/algorith
ms/sorting/quick-sort/visualize/

You might also like