0% found this document useful (0 votes)
24 views

DSAP CH 13 Merge Sort

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

DSAP CH 13 Merge Sort

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

Data Structures and

Algorithms in Python
Michael T. Goodrich/ Roberto
Tamassia/ Michael H. Goldwasser
Adapted by Subhrakanta Panda

Chapter 13
Searching and Sorting

© 2021 Goodrich, Tamassia, Goldwasser


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 2


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 3


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 4


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 5


Python Merge Implementation

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 6


Python Merge-Sort
Implementation

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 7


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


© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 8
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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 10


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 12


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 13


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 14


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 15


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 16


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 17


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

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 18


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

… … …

© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 19


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)
© 2021 Goodrich, Tamassia, Goldwasser Searching and Sorting 20

You might also like