Sort 2 - Merge Sort Mergesort is an excellent method for...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
38 Merge Sort z Mergesort is an excellent method for external sorting , in which the data are kept on disks or magnetic tapes, not in high- speed memory.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
39 Merge Sort z The basic operation in this algorithm is to merge 2 sorted lists. z It takes 2 input arrays A[] & B[], 1 output array C[] and 3 counters for the arrays respectively. z The smaller of A[p1] and B[p2] is copied to C[p3], then the counters are advanced. z If either A[] or B[] finishes first, the reminder of the other array is copied to C[].
Background image of page 2
40 Merge Sort
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
41 Merge Sort
Background image of page 4
42 Merge Sort
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
43 Merge Sort z This algorithm is a classic divide-and- conquer strategy. z The problem is divided into smaller problems and solved recursively. z The conquering phase consists of patching together the answers. z Divide-and-conquer is a very powerful use of recursion that we will see many times.
Background image of page 6
44 Analysis T (1) = 1 T ( n ) = 2 T ( n /2) + n T ( n ) n = T ( n n /2 + 1 T ( n n = T ( n /4) n /4 + 1 T ( n n = T ( n /8) n /8 + 1 M T (2) 2 = T 1 + 1 T ( n ) n = T 1 + log n T ( n ) = n log n + n = O ( n log n )
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
45 QuickSort
Background image of page 8
46 Quicksort z Divide-and-conquer process for sorting a typical subarray A [ p . . r ] z Divide : The array A [ p . . r ] is partitioned (rearranged) into two nonempty subarrays A [ p . . q ] and A [ q + 1 . . r ] such that each element of A [ p . . q ] is less than each element of A [ q + 1 . . r ]. – The index q is computed as part of this partitioning procedure.
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
47 Quicksort z Conquer : The two subarrays A [ p . . q ] and A [ q + 1 . . r ] are sorted by recursive calls to quicksort. z Combine : Since the subarrays are sorted in place, no work is needed to combine them: the entire array A [ p . . r ] is now sorted.
Background image of page 10
48 Example R1 R2 R3 R4 R5 R6 R7 R8 R9 Rl0 m n [26 5 37 1 61 11 59 15 48 19 ] 1 10 [11 5 19 1 15 ] 26 [59 61 48 37 ] 1 5 [ 1 5 ] 11 [19 15 ] 26 [59 61 48 37 ] 1 2 1 5 11 [19 15 ] 26 [59 61 48 37 ] 4 5 1 5 11 15 19 26 [59 61 48 37 ] 7 10 1 5 11 15 19 26 [48 37 ] 59 [61 ] 7 8 1 5 11 15 19 26 37 48 59 [61 ] 10 10 1 5 11 l5 l9 26 37 48 59 6l
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
49 Quicksort Algorithm QUICKSORT( A , p , r ) 1 if p < r 2 then q = PARTITION( A , p , r ) 3 QUICKSORT( A , p , q-1 ) 4 QUICKSORT( A , q + 1, r ) z To sort an entire array A , the initial call is QUICKSORT( A , 1, length[ A ]).
Background image of page 12
50 Quicksort ( partition exchange sort ) z Let x be an array of n elements to be sorted. z Choose an element a from the array. z Then x is partitioned so that a is placed into position j and the following conditions hold: – All the elements from position 0 through j -1 are less than a .
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 38

Sort 2 - Merge Sort Mergesort is an excellent method for...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online