0% found this document useful (0 votes)
136 views19 pages

Goodrich 6e Ch12 MergeSort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views19 pages

Goodrich 6e Ch12 MergeSort

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Presentation for use with the textbook Data Structures and

Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,


and M. H. Goldwasser, Wiley, 2014

Merge Sort
7 29 4  2 4 7 9

72  2 7 94  4 9

77 22 99 44

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 1


Divide-and-Conquer
Divide-and conquer is a Merge-sort is a sorting
general algorithm design algorithm based on the
paradigm: divide-and-conquer
 Divide: divide the input data paradigm
S in two disjoint subsets S1 Like heap-sort
and S2
 It has O(n log n) running
 Recur: solve the time
subproblems associated
with S1 and S2 Unlike heap-sort
 Conquer: combine the  It does not use an
solutions for S1 and S2 into a auxiliary priority queue
solution for S  It accesses data in a
sequential manner
The base case for the (suitable to sort data on a
recursion are subproblems of disk)
size 0 or 1

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 2


Merge-Sort
Merge-sort on an input Algorithm mergeSort(S)
sequence S with n Input sequence S with n
elements consists of elements
three steps: Output sequence S sorted
according to C
 Divide: partition S into
two sequences S1 and S2 if S.size() > 1
of about n/2 elements (S1, S2)  partition(S, n/2)
each mergeSort(S1)
 Recur: recursively sort S1 mergeSort(S2)
and S2 S  merge(S1, S2)
 Conquer: merge S1 and
S2 into a unique sorted
sequence

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 3


Merging Two Sorted Sequences
The conquer step of Algorithm merge(A, B)
merge-sort consists Input sequences A and B with
of merging two n/2 elements each
sorted sequences A Output sorted sequence of A  B
and B into a sorted
sequence S S  empty sequence
containing the union while A.isEmpty()  B.isEmpty()
of the elements of A if A.first().element() < B.first().element()
and B S.addLast(A.remove(A.first()))
Merging two sorted else
sequences, each S.addLast(B.remove(B.first()))
with n/2 elements while A.isEmpty()
and implemented by S.addLast(A.remove(A.first()))
means of a doubly while B.isEmpty()
linked list, takes S.addLast(B.remove(B.first()))
O(n) time return S

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 4


Java Merge Implementation

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 5


Java Merge-Sort
Implementation

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 6


Merge-Sort Tree
An execution of merge-sort is depicted by a binary tree
 each node represents a recursive call of merge-sort and stores
 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

7  2  2 7 9  4  4 9

77 22 99 44


© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 7
Execution Example
Partition
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6

7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 8


Execution Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 9


Execution Example (cont.)
Recursive call, partition
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 10


Execution Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 11


Execution Example (cont.)
Recursive call, base case
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 12


Execution Example (cont.)
Merge
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 13


Execution Example (cont.)
Recursive call, …, base case, merge
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 14


Execution Example (cont.)
Merge
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 8 6

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 15


Execution Example (cont.)
Recursive call, …, merge, merge
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 6 8

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 16


Execution Example (cont.)
Merge
7 2 9 43 8 6 1  1 2 3 4 6 7 8 9

7 29 4 2 4 7 9 3 8 6 1  1 3 6 8

722 7 9 4  4 9 3 8  3 8 6 1  1 6

77 22 99 44 33 88 66 11

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 17


Analysis of Merge-Sort
The height h of the merge-sort tree is O(log n)
 at each recursive call we divide in half the sequence,
The overall amount or work done at the nodes of depth i is O(n)
 we partition and merge 2i sequences of size n/2i
 we make 2i+1 recursive calls
Thus, the total running time of merge-sort is O(n log n)

depth #seqs size


0 1 n

1 2 n/2

i 2i n/2i

… … …

© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 18


Summary of Sorting Algorithms
Algorithm Time Notes
 slow
selection-sort O(n2)  in-place
 for small data sets (< 1K)
 slow
insertion-sort O(n2)  in-place
 for small data sets (< 1K)
 fast
heap-sort O(n log n)  in-place
 for large data sets (1K — 1M)
 fast
merge-sort O(n log n)  sequential data access
 for huge data sets (> 1M)
© 2014 Goodrich, Tamassia, Goldwasser Merge Sort 19

You might also like