Mergesort: Source: Gibbs & Tamassia
Mergesort: Source: Gibbs & Tamassia
MergeSort
MergeSort Algorithm
MergeSort is a recursive sorting procedure that uses at most O(n lg(n)) comparisons. To sort an array of n elements, we perform the following steps in sequence: If n < 2 then the array is already sorted. Otherwise, n > 1, and we perform the following three steps in sequence:
1. Sort the left half of the the array using MergeSort. 2. Sort the right half of the the array using MergeSort. 3. Merge the sorted left and right halves.
How to Merge
Here are two lists to be merged:
First: (12, 16, 17, 20, 21, 27) Second: (9, 10, 11, 12, 19)
Compare12 and 9
First: (12, 16, 17, 20, 21, 27) Second: (10, 11, 12, 19) New: (9)
Compare 12 and 10
First: (12, 16, 17, 20, 21, 27) Second: (11, 12, 19) New: (9, 10)
Merge Example
Compare 12 and 11
First: (12, 16, 17, 20, 21, 27) Second: (12, 19) New: (9, 10, 11)
Compare 12 and 12
First: (16, 17, 20, 21, 27) Second: (12, 19) New: (9, 10, 11, 12)
Merge Example
Compare 16 and 12
First: (16, 17, 20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12)
Compare 16 and 19
First: (17, 20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12, 16)
Merge Example
Compare 17 and 19
First: (20, 21, 27) Second: (19) New: (9, 10, 11, 12, 12, 16, 17)
Compare 20 and 19
First: (20, 21, 27) Second: ( ) New: (9, 10, 11, 12, 12, 16, 17, 19)
Merge Example
Checkout 20 and empty list
First: () Second: ( ) New: (9, 10, 11, 12, 12, 16, 17, 19, 20, 21, 27)
MergeSort
24 Divide in 2 24 Divide in 4 24 Divide in 8 24 Merge 2 13 Merge 4 1 Merge 8 1
Original
13 26 1 12 27 38 15 13 26 1 12 27 38 13 26 1 12 27 13 26 1 24 1 26 13 24 26 12 13 15 24 26 27 38
15 38 15 12 27 38 15 12 27 15 38 12 15 27 38
Merge-Sort Tree
unsorted sequence before the execution and its partition sorted sequence at the end of the execution
the root is the initial call the leaves are calls on subsequences of size 0 or 1
7 2
9 4 2 4 7 9
2 2 7
22
4 4 9
44
77
99
10
Execution Example
Partition
7 2 9 43 8 6 1
11
7 29 4
12
7 29 4
72
13
7 29 4
72
77
14
7 29 4
72
77
22
15
Merge
7 2 9 43 8 6 1
7 29 4
722 7
77
22
16
7 29 4
722 7
9 4 4 9
77
22
99
44
17
Merge
7 2 9 43 8 6 1
7 29 4 2 4 7 9
722 7
9 4 4 9
77
22
99
44
18
7 29 4 2 4 7 9
3 8 6 1 1 3 6 8
722 7
9 4 4 9
3 8 3 8
6 1 1 6
77
22
99
44
33
88
66
11
19
Merge
7 2 9 43 8 6 1 1 2 3 4 6 7 8 9
7 29 4 2 4 7 9
3 8 6 1 1 3 6 8
722 7
9 4 4 9
3 8 3 8
6 1 1 6
77
22
99
44
33
88
66
11
20
Complexity of MergeSort
Pass Number 1 2 3 . . . k = log n k1 k Number of merges 2k-1 or n/2 2k-2 or n/4 2k-3 or n/8 . . . 21 or n/2k-1 20 or n/2k Merge list length 1 or n/2k 2 or n/2k-1 4 or n/2k-2 . . . 2k-2 or n/4 2k-1 or n/2 # of comps / moves per merge 21 22 23 . . . 2k-1 2k
21
Complexity of MergeSort
Multiplying the number of merges by the maximum number of comparisons per merge, we get: (2k-1)21 = 2k k passes each require k-2 2 k (2 )2 =2 k comparisons (and 2 moves). But k = lg n and hence, we get lg(n) n comparisons (21)2k-1 = 2k or O(n lgn) (20)2k = 2k
22