Unit05 3 MergeSort
Unit05 3 MergeSort
Merge Sort
• 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,
Quicksort,
and many, many more
4
Divide
Group of 8
Groups of 4
Groups of 2
Groups of 1
4
Conquer (merge sorted lists)
5
Conquer (merge sorted lists)
6
Merge Sort
17
Merge Outline
Input: Two lists a and b, already sorted
Output: A new list containing the elements of a and b
merged together in sorted order.
Algorithm:
1. Create an empty list c, set index_a and index_b to 0
2. While index_a < length of a and index_b < length of b
a. Add the smaller of a[index_a] and b[index_b]
to the end of c
b. increment the index of the list with the smaller
element
3. If any elements are left over in a or b,
add them to the end of c, in order
4. Return c
11
Filling in the details of Merge
"Add the smaller of a[index_a] and b[index_b] to the end of c,
and increment the index of the list with the smaller element":
11
Filling in the details of Merge
"If any elements are left over in a or b,
add them to the end of c, in order":
b.Otherwise:
11
Merge in Python
def merge(a, b):
index_a = 0
index_b = 0
c = []
while index_a < len(a) and index_b < len(b):
if a[index_a] <= b[index_b]:
c.append(a[index_a])
index_a = index_a + 1
else:
c.append(b[index_b])
index_b = index_b + 1
# when we exit the loop
# we are at the end of at least one of the lists
c.extend(a[index_a:])
c.extend(b[index_b:])
return c 14
Example 1: Merge
list a list b list c
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12 29
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12 29 31
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12 29 31 44
Example 1: Merge (cont’d)
list a list b list c
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12 29 31 44 58
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12 29 31 44 58 62
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
12 44 58 62 29 31 74 80 12 29 31 44 58 62 74 80
Example 2: Merge
list a list b list c
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
58 67 74 90 19 26 31 44 19
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
58 67 74 90 19 26 31 44 19 26
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
58 67 74 90 19 26 31 44 19 26 31
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
58 67 74 90 19 26 31 44 19 26 31 44
0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 7
58 67 74 90 19 26 31 44 19 26 31 44 58 67 74 90
Analyzing Efficiency
Constant time operations:
Comparing values and appending elements to the output.
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?
what is the maximum number of comparisons?
18
How many merges?
• We saw that each group of merges of n elements takes O(n)
operations.
• How many times do we have to merge n elements
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. 5 = log232
– Merge 4 lists of size 8 into 2 lists of size 16.
– Merge 2 lists of size 16 into 1 list of size 32.
• In general: log2n merges of n elements.
20
Big O
21
O(N log N)
(not drawn to scale)
Number of
Operations n log2n = O(n log n)
384
For an n log2 n algorithm,
224 the performance is better
than a quadratic algorithm
but just a little worse than
160 a linear algorithm.
96
64
16 32 64 n
(amount of data)
22
Merge vs. Insertion Sort
8 36 24 0.67
16 136 64 0.47
32 528 160 0.30
210 524,800 10,240 0.02
220 549,756,338,176 20,971,520 0.00004
23
Sorting and Searching
24
Comparing Big O Functions
Number of O(2n) O(n2) O(n log n)
Operations
O(n)
O(log n)
O(1)
n
(amount of data)
25
Merge Sort: Iteratively
(optional)
26
Built-in Sort in Python
• Why we study sorting algorithms
– Practice in algorithmic thinking
– Practice in complexity analysis
27
Quicksort
• Conceptually similar to merge sort
• Uses the technique of divide-and-conquer
1. Pick a pivot
2. Divide the array into two subarrays,
those that are smaller and those that are greater
3. Put the pivot in the middle, between the two sorted
arrays
• Worst case O(n 2)
• "Expected" O(n log n)
28
Next Time
• Data Organization
29