Merge Sort Alexadra Stefa
Merge Sort Divide ad Coquer Tehique Divide ad oquer Divide the problem i smaller problems Solve these problems Combie the aswers Merge sort Split the problem i 2 halves. Sort eah half. Merge the sorted halves. Eah of the three steps will brig a otributio to the time omplexity of the method. Resoures: http://iterativepytho.org/ruestoe/stati/pythods/sortsearh/themerg esort.html https://www.s.usfa.edu/~galles/visualizatio/comparisosort.html 2
Merge sort 0 1 2 3 5 6 7 7 1 3 9 1 8 6 p r q 3
Merge sort The atual sortig is doe whe mergig i this order: 7 1 3 9 1 8 6 7 1 3 9 1 2 1 7 3 9 3 1 3 7 9 1 8 6 5 1 6 8 7 6 1 6 8 1 1 3 6 7 8 9
Merge-Sort Exeutio Eah row shows the array after eah all to the Merge futio fiished. Origial Red items were moved by Merge. 0 1 2 3 5 6 7 7 1 3 9 1 8 6 1 7 3 9 1 8 6 1 7 3 9 1 8 6 1 3 7 9 1 8 6 1 3 7 9 1 8 6 1 3 7 9 1 6 8 1 3 7 9 1 6 8 1 1 3 6 7 8 9 MS(0,7) // q = 3 MS(0,3) // q = 1 MS(0,1) // q = 0 MS(0,0) //p==r, basease MS(1,1) //p==r Merge(0,0,1) MS(2,3) // q = 2 MS(2,2) MS(3,3) Merge(2,2,3) Merge(0,1,3) MS(,7) // q = 5 MS(,5) // q = MS(,) MS(5,5) Merge(,,5) MS(6,7) // q = 6 MS(6,6) MS(7,7) Merge(6,6,7) Merge(,5,7) Merge(0,3,7) Notatio: MS(p,r) for Merge-Sort(A,p,r) 5
Merge sort (CLRS) 7 1 3 9 1 6 8 6
Merge sort (CLRS) What part of the algorithm does the atual sortig (moves the data aroud)? 7 1 3 9 1 6 8 7
Merge sort (CLRS) What is the SPACE omplexity for this lie? How would you implemet this lie? (What C ode would you write?) Look at your ode. What is your spae omplexity? (keep the ostat) 7 1 3 9 1 6 8 8
Merge Sort Is it stable? Variatio that would ot be stable? How muh extra memory does it eed? Pay attetio to the implemetatio! Is it adaptive? Best, worst, average ases? 9
Merge Sort Is it stable? - YES Variatio that would ot be stable? How muh extra memory does it eed? Pay attetio to the implemetatio! Liear: Θ() Extra memory eeded for arrays L ad R i the worst ase is. Note that the extra memory used i merge is freed up, therefore we do ot have to repeatedly add it ad we will NOT get Θ(lg) extra memory. There will be at most lg ope reursive alls. Eah oe of those eeds ostat memory (oe stak frame) => extra memory due to reursio: *lg ( i.e. Θ(lg ) ) Total extra memory: + *lg = Θ(). Is it adaptive? - NO Best, worst, average ases? 10
Time omplexity Let T() be the time omplexity to sort (with merge sort) a array of elemets. Assume is a power of 2 (i.e. = 2 k ). What is the time omplexity to: Split the array i 2: Sort eah half (with MERGESORT): T(/2) Merge the aswers together: (or Θ()) We will see other ways to aswer this questio later. 11
Merge sort (CLRS) Reurree formula Here is the umber of items beig proessed Base ase: T(1) = (I the ode, see for what value of there is NO reursive all. Here whe p<r is false => p r => 1 ) Reursive ase: T() = 2T(/2) + also ok: T() = 2T(/2) + Θ() T(/2) T(/2) 12
Reursio Tree CLRS, page 38. 13
2 2 T ()........................ Reursio Tree Assume that is a power of 2: = 2 k. Number of levels: lg + 1 Eah level has the same ost: Total ost of the tree: (lg + 1)() = lg + = Θ(lg ) 2 2 Level Arg/ pb size Nodes per level 1 ode ost 0 1 Level ost 1 /2 2 /2 2/2 = 2 / / / = i /2 i 2 i /2 i 2 i /2 i k=lg 1 (=/2 k ) 2 k (=) =*1= /2 k = 2 k /2 k 1 =
Reursio Tree - brief Assume that is a power of 2: = 2 k. Number of levels: lg + 1 Eah level has the same ost: Total ost of the tree: (lg + 1)() = lg + = Θ(lg ) 2 2 T () 2..................... 2 2 2... 15
Tree of reursive alls to Merge-Sort MergeSort(A,0,6) proesses the 7 elemets betwee idexes 0 ad 6 (ilusive). The tree below shows all the reursive alls made. N=r-p+1 p 7 (0,6) r (0,3) 3 (,6) 2 (0,1) 2 (2,3) 2 (,5) 1 (6,6) 1 (0,0) 1 (1,1) 1 (2,2) 1 (3,3) 1 (,) 1 (5,5) 16
Mergesort Variatios (Sedgewik) Mergesort with isertio sort for small problem sizes (whe N is smaller tha a ut-off size). The base ase will be at say 10 ad it will ru isertio sort. Bottom-up mergesort, Iterative. Mergesort usig lists ad ot arrays. Both top-dow ad bottom-up implemetatios Sedgewik mergesort uses oe auxiliary array (ot two) Alterate betwee regular array ad the auxiliary oe Will opy data oly oe (ot twie) per reursive all. Costat extra spae (istead of liear extra spae). More ompliated, somewhat slower. Bitoi sequee (first ireasig, the dereasig) Elimiates the idex boudary hek for the subarrays. 17
Merge sort bottom-up
Merge sort bottom-up Notie that after eah pass, subarrays of ertai sizes (2,, 8, 16) or less are sorted. Colors show the subarrays of speifi sizes: 1, 2,, 8, 11. 7 1 3 9 1 6 8 5 15 2 Size 1 1 7 3 9 1 6 8 5 15 2 Size 2 1 3 7 9 1 6 8 2 5 15 Size 1 1 3 6 7 8 9 2 5 15 Size 8 1 1 2 3 5 6 7 8 9 15 Size 16 (11) 19