0% found this document useful (0 votes)
64 views13 pages

Unit 5C Merge Sort: 15110 Principles of Computing, Carnegie Mellon University - CORTINA 1

Merge sort is a divide and conquer algorithm that works by recursively splitting an array into halves, sorting each half, and then merging the sorted halves back together. It has three main steps: 1. Divide: Split the array into equal halves repeatedly until arrays contain a single element. 2. Conquer: Sort the single-element arrays using a comparison-based sorting algorithm. 3. Combine: Merge the sorted halves back together into a single sorted array by comparing elements pair-wise from each half. Merge sort runs in O(n log n) time in all cases making it an efficient general-purpose sorting algorithm. While slower than insertion sort for small inputs, it scales much better for large arrays

Uploaded by

mydummymail
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views13 pages

Unit 5C Merge Sort: 15110 Principles of Computing, Carnegie Mellon University - CORTINA 1

Merge sort is a divide and conquer algorithm that works by recursively splitting an array into halves, sorting each half, and then merging the sorted halves back together. It has three main steps: 1. Divide: Split the array into equal halves repeatedly until arrays contain a single element. 2. Conquer: Sort the single-element arrays using a comparison-based sorting algorithm. 3. Combine: Merge the sorted halves back together into a single sorted array by comparing elements pair-wise from each half. Merge sort runs in O(n log n) time in all cases making it an efficient general-purpose sorting algorithm. While slower than insertion sort for small inputs, it scales much better for large arrays

Uploaded by

mydummymail
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIT 5C Merge Sort

15110 Principles of Computing, Carnegie Mellon University - CORTINA 1

Divide and Conquer


In the military: strategy to gain or maintain power In computation: Divide the problem into simpler versions of itself. Conquer each problem using the same process (usually recursively). Combine the results of the simpler versions to form your final solution. Examples: Towers of Hanoi, fractals, Binary Search, Merge Sort
15110 Principles of Computing, Carnegie Mellon University - CORTINA 2

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.

15110 Principles of Computing, Carnegie Mellon University - CORTINA

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

15110 Principles of Computing, Carnegie Mellon University - CORTINA

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

15110 Principles of Computing, Carnegie Mellon University - CORTINA

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

Example 1: Merge (contd)


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 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 array c 0 1 2 3 4 12 29 31 44 58 5 6 7

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

Required: Two arrays a and b.


Each array must be sorted already in non-decreasing order.

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

Merge in Ruby (contd)


if (index_a < a.length) then for i in (index_a..a.length-1) do c << a[i] end else for i in (index_b..b.length-1) do c << b[i] end end return c end

15110 Principles of Computing, Carnegie Mellon University - CORTINA

13

Merge Sort: Base Case


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. What is the base case? If the list has only 1 element, it is already sorted so just return the list as the result.

15110 Principles of Computing, Carnegie Mellon University - CORTINA

14

Merge Sort: Halfway Point


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. How do we determine the halfway point where we want to split the array list? First half: 0..list.length/2-1 Second half: list.length/2..list.length-1
15110 Principles of Computing, Carnegie Mellon University - CORTINA 15

Merge Sort in Ruby


def msort(list) return list if list.length == 1 # base case halfway = list.length/2 list1 = list[0..halfway-1] list2 = list[halfway..list.length-1] newlist1 = msort(list1) # recursive! newlist2 = msort(list2) # recursive! newlist = merge(newlist1, newlist2) return newlist end
15110 Principles of Computing, Carnegie Mellon University - CORTINA 16

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

How many group merges?


How many group merges does it take to go from n groups of size 1 to 1 group of size n? Example: Merge sort on 32 elements.
Break down to groups of size 1 (base case). Merge 32 lists of size 1 into 16 lists of size 2. Merge 16 lists of size 2 into 8 lists of size 4. Merge 8 lists of size 4 into 4 lists of size 8. Merge 4 lists of size 8 into 2 lists of size 16. Merge 2 lists of size 16 into 1 list of size 32.

5 = log232

In general: log2n group merges must occur.


15110 Principles of Computing, Carnegie Mellon University - CORTINA 18

Putting it all together


It takes log2n iterations to go from n groups of size 1 to a single group of size n. Total number of elements per level is always n.

It takes n appends to merge all pairs to the next higher level.


15110 Principles of Computing, Carnegie Mellon University - CORTINA 19

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)

15110 Principles of Computing, Carnegie Mellon University - CORTINA

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

Comparing Insertion Sort to Merge Sort


(Worst Case)

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

Sorting and Searching


Recall that if we wanted to use binary search, the array must be sorted.
What if we sort the array first using merge sort?
Merge sort Binary search Total time: (worst case) O(n log n) (worst case) O(log n) (worst case) O(n log n) + O(log n) = O(n log n)

15110 Principles of Computing, Carnegie Mellon University - CORTINA

23

Comparing Big O Functions


Number of Operations O(2n) O(n2) O(n log n) O(n)

O(log n) O(1) n (amount of data)


15110 Principles of Computing, Carnegie Mellon University - CORTINA 24

12

Merge Sort: Iteratively


(optional)

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.

15110 Principles of Computing, Carnegie Mellon University - CORTINA

25

13

You might also like