Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Red-Black Trees 二○一七年四月十二日 AVL Trees v z AVL Trees.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Goodrich, Tamassia Sorting, Sets and Selection1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures Lecture 9 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
Merge Sort1 Part-G1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
CSC2100B Quick Sort and Merge Sort Xin 1. Quick Sort Efficient sorting algorithm Example of Divide and Conquer algorithm Two phases ◦ Partition phase.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
© 2004 Goodrich, Tamassia Sets1. © 2004 Goodrich, Tamassia Sets2 Set Operations (§ 10.6) We represent a set by the sorted sequence of its elements By.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4 Modified by: Daniel Gomez-Prado, University of Massachusetts Amherst.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Sorting.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Lecture10: Sorting II Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Merge Sort1 7 2  9 4   2  2 79  4   72  29  94  4.
1 More Merge Sort Java implementation Time complexity Generic merging and sets.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
Towers of Hanoi Move n (4) disks from pole A to pole B such that a larger disk is never put on a smaller disk A BC ABC.
1 COMP9024: Data Structures and Algorithms Week Eight: Sortings and Sets Hui Wu Session 1, 2016
Sets and Multimaps 9/9/2017 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
Advanced Sorting 7 2  9 4   2   4   7
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Merge Sort 7 2  9 4   2   4   7 2  2 9  9 4  4.
Merge Sort 1/12/2018 9:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Merge Sort 1/12/2018 9:44 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Sets and Multimaps 5/9/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Divide-and-Conquer 6/30/2018 9:16 AM
Quick Sort and Merge Sort
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Quick-Sort 9/12/2018 3:26 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
MergeSort Source: Gibbs & Tamassia.
More on Merge Sort CS 244 This presentation is not given in class
Radish-Sort 11/11/ :01 AM Quick-Sort     2 9  9
Objectives Introduce different known sorting algorithms
Sequences 11/12/2018 5:23 AM Sets Sets.
Merge Sort 12/4/ :32 AM Merge Sort 7 2   7  2   4  4 9
Divide-and-Conquer 7 2  9 4   2   4   7
Sorting Recap & More about Sorting
Sorting, Sets and Selection
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Divide-and-Conquer 7 2  9 4   2   4   7
Sequences 4/10/2019 4:55 AM Sets Sets.
Merge Sort 4/10/ :25 AM Merge Sort 7 2   7  2   4  4 9
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Divide & Conquer Sorting
Divide-and-Conquer 7 2  9 4   2   4   7
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
Divide-and-Conquer 7 2  9 4   2   4   7
Chapter 11 Sets, and Selection
Presentation transcript:

Merge Sort 2/24/2019 6:07 PM 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 2/24/2019 6:07 PM Merge Sort

Outline and Reading Divide-and-conquer paradigm Merge-sort (§10.1) Algorithm Merging two sorted sequences Merge-sort tree Execution example Analysis Generic merging and set operations Summary of sorting algorithms 2/24/2019 6:07 PM Merge Sort

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

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

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

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 2/24/2019 6:07 PM Merge Sort

Execution Example Partition 7 2 9 4  3 8 6 1  1 2 3 4 6 7 8 9 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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 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 2/24/2019 6:07 PM Merge Sort

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 2/24/2019 6:07 PM Merge Sort

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 1 n 2 n/2 i 2i n/2i … 2/24/2019 6:07 PM Merge Sort

Generic Merging Generalized merge of two sorted sequences A and B Template method genericMerge Auxiliary methods aIsLess bIsLess bothAreEqual Runs in O(nA + nB) time provided the auxiliary methods run in O(1) time Algorithm genericMerge(A, B) S  empty sequence while A.isEmpty()  B.isEmpty() a  A.first().element(); b  B.first().element() if a < b aIsLess(a, S); A.remove(A.first()) else if b < a bIsLess(b, S); B.remove(B.first()) else { b = a } bothAreEqual(a, b, S) A.remove(A.first()); B.remove(B.first()) while A.isEmpty() while B.isEmpty() return S 2/24/2019 6:07 PM Merge Sort

Set Operations We represent a set by the sorted sequence of its elements By specializing the auxliliary methods the generic merge algorithm can be used to perform basic set operations: union intersection subtraction The running time of an operation on sets A and B is O(nA + nB) Set union: aIsLess(a, S) S.insertFirst(a) bIsLess(b, S) S.insertLast(b) bothAreEqual(a, b, S) S. insertLast(a) Set intersection: { do nothing } 2/24/2019 6:07 PM Merge Sort

Summary of Sorting Algorithms Time Notes selection-sort O(n2) slow in-place for small data sets (< 1K) insertion-sort heap-sort O(n log n) fast for large data sets (1K — 1M) merge-sort sequential data access for huge data sets (> 1M) 2/24/2019 6:07 PM Merge Sort