Merge Sort
Merge Sort
Merge Sort
STRUCTURES
MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com
MERGE
SORT
DIVIDE & CONQUER
Binary Search
Search
Search
Search
DIVIDE & CONQUER
Sort
Sort Sort
Combine
Combine Combine
MergeSort Cost (real order)
Four Recursive Sorts
Size of Sublists
n/2,n/2 n-1,1
In plain English: if the size of the array > 1, split the array into
two halves, and recursively sort both halves; when the sorts return,
merge the two halves
In pseudocode:
if size equals 1
return
copy first half into leftArray
copy second half into rightArray
sort (leftArray)
sort (rightArray)
merge leftArray with rightArray
MERGE SORT
25,31,52,88,98 14,23,30,62,79
Merge Sort
Merge two sorted lists into one
25,31,52,88,98
14,23,25,30,31,52,62,79,88,98
14,23,30,62,79
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
aSize: 5 bSize: 6
tmp:
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=0 j=0
tmp:
k=0
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=0 j=0
tmp: 3
k=0
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=1 j=0
tmp: 3 5
k=1
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=0
tmp: 3 5 6
k=2
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=1
tmp: 3 5 6 10
k=3
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=2
tmp: 3 5 6 10 14
k=4
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=2 j=3
tmp: 3 5 6 10 14 15
k=5
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=3 j=3
tmp: 3 5 6 10 14 15 22
k=6
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=3 j=4
tmp: 3 5 6 10 14 15 22 28
k=7
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=4 j=4
tmp: 3 5 6 10 14 15 22 28 30
k=8
MERGE SORT
a: 3 5 15 28 30 b: 6 10 14 22 43 50
i=5 j=4
Done.
tmp: 3 5 6 10 14 15 22 28 30 43 50
k=9
MERGE SORT
Sort Sort
Merge
COMPLEXITY
At each level of recursion, all of the elements are copied; thus the
decomposition phase takes n log n time
At each level of recursion, all of the elements are copied in the merge
operation; thus the merge phase takes n log n time
T = Td + Tm = n log n + n log n = 2 (n log n )
Merge sort is O(n log n )
Merging two sorted arrays
20 12
13 11
7 9
2 1
Merging two sorted arrays
20 12
13 11
7 9
2 1
1
Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2
1
Merging two sorted arrays
20 12 20 12
13 11 13 11
7 9 7 9
2 1 2
1 2
Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2
1 2
Merging two sorted arrays
20 12 20 12 20 12
13 11 13 11 13 11
7 9 7 9 7 9
2 1 2
1 2 7
Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7
Merging two sorted arrays
20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11 12
Merging two sorted arrays
20 12 20 12 20 12 20 12 20 12 20 12
13 11 13 11 13 11 13 11 13 11 13
7 9 7 9 7 9 9
2 1 2
1 2 7 9 11 12
cn
T(n/2) T(n/2)
cn
cn/2 cn/2
(1)
cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4
…
(1)
cn cn
cn/2 cn/2
h = lg n cn/4 cn/4 cn/4 cn/4
…
(1)
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4
…
(1)
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
…
(1)
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
…
(1) #leaves = n (n)
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn/4 cn/4 cn
…
…
(1) #leaves = n (n)
Total(n log n)