1 / 14

Merge sort

Merge sort. csc326 information structures Spring 2009. Mergesort. Based on divide-and-conquer strategy Divide the list into two smaller lists of about equal sizes Sort each smaller list recursively Merge the two sorted lists to get one sorted list

gizi
Télécharger la présentation

Merge sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Merge sort csc326 information structures Spring 2009

  2. Mergesort Based on divide-and-conquer strategy • Divide the list into two smaller lists of about equal sizes • Sort each smaller list recursively • Merge the two sorted lists to get one sorted list How do we divide the list? How much time is needed? How do we merge the two sorted lists? How much time is needed?

  3. Dividing • It is assumed that the input list is an array A[0..N-1]. • Dividing the array takes O(1) time. • We can represent a sublist by two integers left and right: to divide A[left..Right], we compute center=(left+right)/2 and obtain A[left..Center] and A[center+1..Right].

  4. Mergesort • Divide-and-conquer strategy • recursively mergesort the first half and the second half • merge the two sorted halves together

  5. see applet http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/MSort.html

  6. How to merge? • Input: two sorted arrays A and B • Output: an output sorted array C • Three counters: Actr, Bctr, and Cctr • initially set to the beginning of their respective arrays (1)The smaller of A[Actr] and B[Bctr] is copied to the next entry in C, and the appropriate counters are advanced (2)When either input list is exhausted, the remainder of the other list is copied to C

  7. Example: Merge

  8. Example: Merge... • Running time analysis: • Clearly, merge takes O(m1 + m2) where m1 and m2 are the sizes of the two sublists. • Space requirement: • merging two sorted lists requires linear extra memory • additional work to copy to the temporary array and back

  9. Note: p=Actr, q=Bctr, i = Cctr

  10. Analysis of mergesort (informal) The height of the tree is O(log n) For each level, all the elements will be merged(in separate merge groups or in the same merge group), so all n elements will be visited once for each level. Totally all n elements will be visited O(log n) times. So the total running time is O(nlog n).

  11. Analysis of mergesort (formal)not required Let T(N) denote the worst-case running time of mergesort to sort N numbers. Assume that N is a power of 2. • Divide step: O(1) time (assume array implementation) • Conquer step: 2 T(N/2) time • Combine step: O(N) time (because two sublists of size N/2 are merged) Recurrence for T(N): T(1) = 1 T(N) = 2T(N/2) + N

  12. Analysis: solving the recurrence (not required) Since N=2k, we have k=log2 N

  13. An experiment • Code from textbook (using template) • Unix/Linux time utility

More Related