Вы находитесь на странице: 1из 49

DATA

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

Sort Sort Sort Sort


DIVIDE & CONQUER

Combine

Combine Combine
MergeSort Cost (real order)
Four Recursive Sorts
Size of Sublists
n/2,n/2 n-1,1

Minimal effort splitting


Merge Sort Insertion Sort
Lots of effort recombining

Lots of effort splitting


Minimal effort recombining
Quick Sort Selection Sort
TERMINOLOGY

 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

 Mergesort is a divide and conquer algorithm that does exactly that.


 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
Merge Sort
88 52
14
31
25 98 30
62 23
79

Divide and Conquer


Merge Sort
88 52 Split Set into Two
14
31
25 98 30 (no real work)
62 23
79

sort the first half. sort the second half.

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

Time = (n) to merge a total


of n elements (linear time).
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn
T(n/2) T(n/2)
cn
cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)


cn
cn/2 cn/2

cn/4 cn/4 cn/4 cn/4


(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)

Вам также может понравиться