Your SlideShare is downloading. ×
Merge sort
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Merge sort

2,521
views

Published on

Published in: Education, Technology, Business

1 Comment
3 Likes
Statistics
Notes
  • Hi... If you could explain how did u obtain O(nlogn) i.e How to obtain best case, worst case and avg case for any algo???
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,521
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
179
Comments
1
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Algorithm Design & AnalysisPresentation By: NUPUR
  • 2. Invented byJohn von Neumann (1903-1957) Follows divide and conquer paradigm. Developed merge sort for EDVAC in 1945
  • 3.  The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1≤x2≤…≤xm) and Y(y1≤y2≤…≤yn) the resulting list is Z(z1≤z2≤…≤zm+n) Example:L1 = { 3 8 9 } L 2 = { 1 5 7 }merge(L1, L2) = { 1 3 5 7 8 9 }
  • 4. 1.Divide: Divide the unsorted list into two sub lists of about half the size.2.Conquer: Sort each of the two sub lists recursively until we have list sizes of length 1,in which case the list itself is returned.3.Combine: Merge the two-sorted sub lists back into one sorted list.
  • 5. Merge-Sort (A, n) if n=1 return else n1 ← n2 ← n/2 create array L[n1], R[n2] for i ← 0 to n1-1 do L[i] ← A[i] for j ← 0 to n2-1 do R[j] ← A[n1+j]  Merge-Sort(L, n1)  Merge-Sort(R, n2)  Merge(A, L, n1, R, n2 )
  • 6. 99 6 86 15 58 35 86 4 0
  • 7. 99 6 86 15 58 35 86 4 099 6 86 15 58 35 86 4 0
  • 8. 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 099 6 86 15 58 35 86 4 0
  • 9. 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 099 6 86 15 58 35 86 4 0
  • 10. 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 0 99 6 86 15 58 35 86 4 099 6 86 15 58 35 86 4 0 4 0
  • 11. 99 6 86 15 58 35 86 0 4Merge 4 0
  • 12. 6 99 15 86 58 35 0 4 8699 6 86 15 58 35 86 0 4Merge
  • 13. 6 15 86 99 0 4 35 58 86 6 99 15 86 58 35 0 4 86Merge
  • 14. 0 4 6 15 35 58 86 86 99 6 15 86 99 0 4 35 58 86Merge
  • 15. 0 4 6 15 35 58 86 86 99
  • 16.  There are two basic ways to implement merge sort: In Place: Merging is done with only the input array Double Storage: Merging is done with a temporary array of the same size as the input array.
  • 17. Merge-Sort Analysis • Time, merging n n/2 n/2 log n levels n/4 n/4 n/4 n/4 Total time for merging: cn log n • Total running time: order of nlogn • Total Space: order of n 17
  • 18.  Merge sort’s merge operation is useful in online sorting, where the list to be sorted is received a piece at a time,instead of all at the beginning..In this We sort each new piece that is received using any sorting algorithm, and then merge it into our sorted list so far using the merge operation.
  • 19. Best Case, Average Case, and Worst Case = O(NlogN)• Storage Requirement:Double that needed to hold the array to be sorted.

×