09. Merge Sort
-
Upload
akif-vohra -
Category
Documents
-
view
224 -
download
0
Transcript of 09. Merge Sort
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 1/49
DATASTRUCTURE
S
MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 3/49
SearchSearch
SearchSearch
SearchSearch
Binary Search
IVIDE & CONQUER
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 4/49
SortSort
SortSort SortSort
SortSort SortSort SortSort SortSort
IVIDE & CONQUER
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 5/49
IVIDE & CONQUER
CombineCombine
CombineCombine CombineCombine
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 6/49
MergeSort Cost (real order)MergeSort Cost (real order)
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 7/49
Minimal effort splitting
Lots of effort recombining
Lots of effort splitting
Minimal effort recombining
Four Recursive Sorts
Merge Sort Insertion Sort
Quick Sort Selection Sort
Size of Sublists
n/2,n/2 n-1,1
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 8/49
In plain English: if the size of the array > 1, split thearray 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
TERMINOLOGY
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 9/49
MERGE SORT
Mergesort is a divide and conquer algorithm that does exactlythat.
It splits the list in half
It sorts the two halves
Then merges the two sorted halves together Mergesort can be implemented recursively
The merge sort algorithm involves three steps:
If the number of items to sort is 0 or 1, return
Recursively sort the first and second halves separately
Merge the two sorted halves into a sorted group
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 10/49
Merge SortMerge Sort
8814
982562
52
79
3023
31
Divide and Conquer
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 11/49
Merge SortMerge Sort
8814
982562
52
79
3023
31Split Set into Two
(no real work)
25,31,52,88,98
sort the first half.
14,23,30,62,79
sort the second half.
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 12/49
Merge SortMerge Sort
Merge two sorted lists into one
25,31,52,88,98
14,23,30,62,79
14,23,25,30,31,52,62,79,88,98
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 14/49
MERGE SORT
3 5 15 28 30 6 10 14 22 43 50a:a: b: b:
aSize: 5aSize: 5 bSize: 6 bSize: 6
tmp:tmp:
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 15/49
MERGE SORT
5 15 28 30 10 14 22 43 50a:a: b: b:
tmp:tmp:
i=0i=0
k=0k=0
j=0j=0
3 6
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 16/49
MERGE SORT
5 15 28 30 10 14 22 43 50a:a: b: b:
tmp:tmp:
i=0i=0
k=0k=0
3
j=0j=0
3 6
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 17/49
MERGE SORT
3 15 28 30 10 14 22 43 50a:a: b: b:
tmp:tmp:
i=1i=1 j=0j=0
k=1k=1
3 5
5 6
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 18/49
MERGE SORT
3 5 28 30 10 14 22 43 50a:a: b: b:
3 5tmp:tmp:
i=2i=2 j=0j=0
k=2k=2
6
15 6
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 19/49
MERGE SORT
3 5 28 30 6 14 22 43 50a:a: b: b:
3 5 6tmp:tmp:
i=2i=2 j=1j=1
k=3k=3
15 10
10
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 20/49
MERGE SORT
10
3 5 28 30 6 22 43 50a:a: b: b:
3 5 6tmp:tmp:
i=2i=2 j=2j=2
k=4k=4
15 10 14
14
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 21/49
MERGE SORT
1410
3 5 28 30 6 14 43 50a:a: b: b:
3 5 6tmp:tmp:
i=2i=2 j=3j=3
k=5k=5
15 10 22
15
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 22/49
MERGE SORT
1410
3 5 30 6 14 43 50a:a: b: b:
3 5 6tmp:tmp:
i=3i=3 j=3j=3
k=6k=6
15 10 22
2215
28
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 23/49
MERGE SORT
1410
3 5 30 6 14 50a:a: b: b:
3 5 6tmp:tmp:
i=3i=3 j=4j=4
k=7k=7
15 10 22
2815
28 43
22
G SO
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 24/49
MERGE SORT
1410
3 5 6 14 50a:a: b: b:
3 5 6tmp:tmp:
i=4i=4 j=4j=4
k=8k=8
15 10 22
3015
28 43
22
30
28
MERGE SORT
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 25/49
MERGE SORT
1410
3 5 6 14 50a:a: b: b:
3 5 6 30tmp:tmp:
i=5i=5 j=4j=4
k=9k=9
15 10 22
15
28 43
22
30
28 43 50
Done.
MERGE SORT
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 26/49
MERGE SORT
Sort Sort
Merge
COMPLEXITY
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 27/49
At each level of recursion, all of the elements are copied; thus thedecomposition phase takes n log n time
At each level of recursion, all of the elements are copied in themerge 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
)
COMPLEXITY
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 28/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 29/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 30/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 31/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 32/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 33/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 34/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 35/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
9
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 36/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
9
20
13
12
11
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 37/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
9
20
13
12
11
11
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 38/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
9
20
13
12
11
11
20
13
12
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 39/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
9
20
13
12
11
11
20
13
12
12
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 40/49
Merging two sorted arraysMerging two sorted arrays
20
13
7
2
12
11
9
1
1
20
13
7
2
12
11
9
2
20
13
7
12
11
9
7
20
13
12
11
9
9
20
13
12
11
11
20
13
12
12
Time = Θ (n) to merge a
total of n elements (linear
time).
Recursion tree
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 41/49
Recursion tree
Solve T (n) = 2T (n/2) + cn, where c > 0 is constant.
T (n/2) T (n/2)
cn
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 42/49
cn
T (n/4) T (n/4) T (n/4) T (n/4)
cn/2 cn/2
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 43/49
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
Θ (1) …
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 44/49
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
Θ (1) …
h = lg n
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 45/49
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
Θ (1) …
h = lg n
cn
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 46/49
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
Θ (1) …
h = lg n
cn
cn
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 47/49
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
Θ (1) …
h = lg n
cn
cn
cn
…
8/8/2019 09. Merge Sort
http://slidepdf.com/reader/full/09-merge-sort 48/49
cn
cn/4 cn/4 cn/4 cn/4
cn/2 cn/2
Θ (1) …
h = lg n
cn
cn
cn
#leaves = n Θ (n) …