















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















































1
Presentation for use with the textbook, Algorithm Design andApplications, by M. T. Goodrich and R. Tamassia, Wiley, 2015
2
S and in two disjoint subsets S 2 S 1 Conquer: Recur: solve the subproblems associatedwith S 1 and S 2 Combine: make thesolutions for into a solution for S 1 and S S 2
1
2
decreasing orderDivide-and-Conquer: If n=1 terminate (every one-element list is alreadysorted) If n>1, partition elements into two or more sub-collections; sort each; combine into a single sorted How do we partition?list
First n-1 elements into set A, last element into set BSort A using this partitioning scheme recursively Combine A and B using method Insert() (= insertion^ ^ B already sorted into sorted array)Leads to recursive version of InsertionSort() Number of comparisons: O(n Best case = n-1^2 )
3
4
7
It accesses data in a sequential manner (suitable to sort data on a disk)
The key to Merge Sort is merging two sorted lists intoone, such that if you have two sorted lists X = (xZ = (z 1 x 2 1 …z 2 xm… ) and Y= (yz (^) m+n) 1 y 2 …y (^) n), the resulting list is Example:L merge(L^1 = { 3 8 9 } 1 , L 2 ) = { 1 3 5 7 8 9 }^ L^2 = { 1 5 7 }
7
8
Result:
X: Y: Result:
9
10
X: Y: Result:
X: Y: Result:
13
14
X: Y: Result:
X: Y: Result:
15
16
There are two basic ways to implement merge sort: In Place: Merging is done with only the input array Pro: Requires only the space needed to hold the arrayCon: Takes longer to merge because if the next element is in the right side then all of the elementsmust be moved down. Double Storage: Merging is done with a temporary arrayof the same size as the input array. Pro: Faster than In Place since the temp array holdsthe resulting array until both left and right sides are merged into the temp array, then the temp array isappended over the input array. Con: The memory requirement is doubled.
20
An execution of merge-sort is depicted by a binary tree each node represents a recursive call of merge-sort and stores unsorted sequence before the execution and its partitionsorted sequence at the end of the execution
7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4
19
20
21
22
21
22
25
26
25
26
27
28
27
28
31
Merge-sort on an inputsequence S with n elements consists ofthree steps:
32
depth #seqs 0 1 size n 1 2 n 2 i 2 i^ n 2 i … … …
31
32
T(n/2) = 2T(n/4) + c… 2 n/ T(n) = 4T(n/4) + 2 cT(n) = 8T(n/8) + 3 c 22 n n …T(n) = 2i (^) T(n/2 i (^) ) + ic 2 n Assuming n = 2side; this happens when i=kk^ , expansion halts when we get T(1) on right T(n) = 2k (^) T(1) + kc 2 n Since 2T(n) = c k^ =n, we know k=log(n); since T(1) = c 1 n + c 2 nlogn; 1 , we get thus an upper bound for TmergeSort (n) is O(nlogn)
33
34
Phase 1: PREPARE. Read a page, sort it, write it.
Phase 2, 3, …, etc.: MERGE: Three buffer pages used.
Main memory buffers
INPUT 1 INPUT 2 OUTPUT Disk Disk
Disk
input Main memory Disk
1 buffer 1 buffer 1 buffer
37
38
Idea: conquer: Divide and sort
Input file 1-page runs 2-page runs 4-page runs
8-page runs
PASS 0 PASS 1 PASS 2 PASS 3
9
3,4 6,2^ 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3^2 2,34,6 4,78,9 5,61,3 2 2,34, 6,78,9 1,23,5 6 1,22, 3,44, 6,67,
Costs for one pass:read/write all pages (^) height of tree# of passes : Total cost :product of above
Input file 1-page runs 2-page runs 4-page runs
8-page runs
PASS 0 PASS 1 PASS 2 PASS 3
9
3,4 6,2^ 9,4 8,7 5,6 3,1 2 3,4 2,6 4,9 7,8 5,6 1,3^2 2,34,6 4,78,9 5,61,3 2 2,34, 6,78,9 1,23,5 6 1,22, 3,44,
39
40