Unit 5C Merge Sort: 15110 Principles of Computing, Carnegie Mellon University - CORTINA 1
Unit 5C Merge Sort: 15110 Principles of Computing, Carnegie Mellon University - CORTINA 1
Merge Sort
Required: Array A of n elements. Result: Returns a new array containing the same elements in non-decreasing order. General algorithm for merge sort: 1. Sort the first half using merge sort. (recursive!) 2. Sort the second half using merge sort. (recursive!) 3. Merge the two sorted halves to obtain the final sorted array.
Divide (Split)
84 27 49 91 32 53 63 17
84
27
49
91
32
53
63
17
84 84
27 27
49 49
91 91
32 32
53 53
63 63
17 17
4
Conquer (Merge)
17 27 32 49 53 63 84 91
27
49
84
91
17
32
53
63
27 84
84 27
49 49
91 91
32 32
53 53
17 63
63 17
5
Example 1: Merge
array a 0 1 2 3 12 44 58 62 0 1 2 3 12 44 58 62 0 1 2 3 12 44 58 62 0 1 2 3 12 44 58 62 array b 0 1 2 3 29 31 74 80 0 1 2 3 29 31 74 80 0 1 2 3 29 31 74 80 0 1 2 3 29 31 74 80 array c 0 1 2 12 0 1 12 29 2 3 4 5 6 7
0 1 2 12 29 31
0 1 2 3 12 29 31 44
0 1 2 3 4 5 12 29 31 44 58 62
0 1 2 3 4 5 6 7 12 29 31 44 58 62 74 80
Example 2: Merge
array a 0 1 2 3 58 67 74 90 0 1 2 3 58 67 74 90 0 1 2 3 58 67 74 90 0 1 2 3 58 67 74 90 0 1 2 3 58 67 74 90 array b 0 1 2 3 19 26 31 44 0 1 2 3 19 26 31 44 0 1 2 3 19 26 31 44 0 1 2 3 19 26 31 44 0 1 2 3 19 26 31 44 array c 0 1 2 19 0 1 19 26 2 3 3 3 4 4 4 4 5 5 5 5 6 6 6 6 7 7 7 7
0 1 2 19 26 31
0 1 2 3 19 26 31 44
0 1 2 3 4 5 6 7 19 26 31 44 58 67 74 90
Merge
Result: Returns a new array containing the same elements merged together into a new array in nondecreasing order. Well need two variables to keep track of where we are in arrays a and b: index_a and index_b. 1. Set index_a equal to 0. 2. Set index_b equal to 0. 3. Create an empty array c.
15110 Principles of Computing, Carnegie Mellon University - CORTINA 9
Merge (contd)
4. While index_a < the length of array a and index_b < the length of array b, do the following: a. If a[index_a] b[index_b], then do the following: i. append a[index_a] on to the end of array c ii. add 1 to index_a Otherwise, do the following: i. append b[index_b] on to the end of array c ii. add 1 to index_b
15110 Principles of Computing, Carnegie Mellon University - CORTINA
10
Merge (contd)
(Once we finish step 4, weve added all of the elements of either array a or array b to array c. The other array still has some elements left that need to be added to array c.)
5. If index_a < the length of array a, then: append all remaining elements of array a on to the end of array c Otherwise: append all remaining elements of array b on to the end of array c 6. Return array c as the result.
15110 Principles of Computing, Carnegie Mellon University - CORTINA 11
Merge in Ruby
def merge(a, b) index_a = 0 index_b = 0 c = [] while index_a < a.length and index_b < b.length do if a[index_a] <= b[index_b] then c << a[index_a] index_a = index_a + 1 else c << b[index_b] index_b = index_b + 1 end end
15110 Principles of Computing, Carnegie Mellon University - CORTINA 12
13
14
Analyzing Efficiency
If you merge two lists of size i/2 into one new list of size i, what is the maximum number of appends that you must do? Clearly, each element must be appended to the new list at some point, so the total number of appends is i. If you have a set of pairs of lists that need to be merged (two pairs at a time), and the total number of elements in all of the lists combined is n, the total number of appends will be n.
15110 Principles of Computing, Carnegie Mellon University - CORTINA 17
5 = log232
Big O
In the worst case, merge sort requires O(n log n) time to sort an array with n elements. Number of operations
n log2n 4n log10n n log2n + 2n
Order of Complexity
O(n log n) O(n log n) O(n log n)
20
10
O(N log N)
Number of Operations (not drawn to scale) n log2n = O(n log n)
384
224 For an n log2 n algorithm, the performance is better than a quadratic algorithm but just a little worse than a linear algorithm.
160 64
96
16
15-105 Principles of Computation, Carnegie Mellon University CORTINA
32
64
n (amount of data)
21
isort (n(n+1)/2)
msort (n log2n)
8 36 24 16 136 64 32 528 160 210 524,800 10,240 220 549,756,338,176 20,971,520 For array sizes less than 100, theres not much difference between these sorts, but for larger arrays sizes, there is a clear advantage to merge sort.
15110 Principles of Computing, Carnegie Mellon University - CORTINA 22
11
23
12
If you are interested, the textbook discusses an iterative version of merge sort which you can read on your own. This version uses an alternate version of the merge function that is not shown in the textbook but is given in the RubyLabs gem.
25
13