Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Like this presentation? Why not share!

- Insertion and Deletion in Binary Se... by Lovelyn Rose 13846 views
- Mergesort - How recursion works? by Lovelyn Rose 2694 views
- Merge sort: illustrated step-by-ste... by Yoshi Watanabe 4469 views
- Merge sort by Vidushi Pathak 7574 views
- Problem solving by Lovelyn Rose 541 views
- 16 mergesort by Xavient Informati... 425 views

1,087 views

Published on

No Downloads

Total views

1,087

On SlideShare

0

From Embeds

0

Number of Embeds

23

Shares

0

Downloads

37

Comments

0

Likes

1

No embeds

No notes for slide

- 1. MERGE SORT - HOW RECURSION WORKS 10 4 126152 2 4 6 10 12 15 Dr.S.Lovelyn Rose PSGCollege ofTechnology Coimbatore, India
- 2. Splitting Solve subproblems Combine solution ALGORITHM MergeSort(low,high) { if(low<high) { mid=(low+high)/2 MergeSort(low,mid) MergeSort(mid+1,high) Merge(low,mid,high) } }
- 3. Merge(low,mid,high) { h=low; i=low; j=mid+1 while((h<=mid) and (j<=high)) { if(a[h]<=a[j]) { b[i]=a[h] h=h+1 } else { b[i]=a[j] j=j+1 } i=i+1 }//End of while temp array 2nd sub array1st sub array
- 4. temp to original array for k=low to high a[k]=b[k] } If(h>mid) { for k=j to high { b[i]=a[k] i=i+1 } } else { for k=h to mid { b[i]=a[k] i=i+1 } }
- 5. Recursion Order 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 8 3 2 9 7 1 5 4 3 8 4 51 72 9 2 3 8 9 1 4 5 7 1 2 3 4 5 7 8 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Note :The numbers in red help to understand the order in which recursion takes place
- 6. Formulating the Recurrence Relation T(n)=2T(n/2)+(n-1) Size of a sublist Number of sublists (n-1) Number of times basic operation is performed when a function is called Inside Merge() a minimum of (n-1) comparisons is performed. Eg. If 1 element no comparison If 2 elements->max 1 comparison If 4 elements->3 comparisons
- 7. Solving Recurrence MastersTheorem a=2,b=2,f(n)=n-1 log b a=1 By case 2,f(n)=Ѳ(n1 )=O(n) T(n)= Ѳ(n log n)
- 8. By Substitution Method T(n)=2T(n/2)+(n-1) =2(2T(n/22 )+((n/2)-1))+(n-1) =22 T(n/22 )+(n-2)+(n-1) =22 (2T(n/23 )+((n/4)-1))+(2n-(1+2)) =23T (n/23 )+3n-(1+2+22 ) . . =2k T(n/2k )+kn-(1+2+…+2k-1 ) Let n=2k =2k T(1 )+n log n-(2k -1 ) =n log n-n+1 = Ѳ(n log n)
- 9. Space Complexity = n+n = 2n = Ѳ(n) Additional array ‘b’ of size ‘n’ Input Array of size ‘n’
- 10. My Blogs http://datastructuresinterview.blogspot.in/ http://talkcoimbatore.blogspot.in/ http://simpletechnical.blogspot.in/

No public clipboards found for this slide

×
### Save the most important slides with Clipping

Clipping is a handy way to collect and organize the most important slides from a presentation. You can keep your great finds in clipboards organized around topics.

Be the first to comment