Presentation is loading. Please wait.

Presentation is loading. Please wait.

Merge sort csc326 information structures Spring 2009.

Similar presentations


Presentation on theme: "Merge sort csc326 information structures Spring 2009."— 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 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: n merging two sorted lists requires linear extra memory n 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=2 k, we have k=log 2 N

13

14 An experiment Code from textbook (using template) Unix/Linux time utility


Download ppt "Merge sort csc326 information structures Spring 2009."

Similar presentations


Ads by Google