1 More Merge Sort Java implementation Time complexity Generic merging and sets.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Back to Sorting – More efficient sorting algorithms.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Garfield AP Computer Science
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.
A simple example finding the maximum of a set S of n numbers.
© 2004 Goodrich, Tamassia Merge Sort1 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 4/15/2017 4:30 PM Merge Sort 7 2   7  2  2 7
Using Divide and Conquer for Sorting
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.
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.
© 2004 Goodrich, Tamassia Sets1. © 2004 Goodrich, Tamassia Sets2 Set Operations (§ 10.6) We represent a set by the sorted sequence of its elements By.
© 2004 Goodrich, Tamassia Merge Sort1 Quick-Sort     29  9.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
CSC 213 Lecture 15: Sets, Union/Find, and the Selection Problem.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
SEQUENCES SORTING Merge, Quick, Bucket, Heap, Radix.
Sorting Fun1 Chapter 4: Sorting     29  9.
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.
Quick sort, lower bound on sorting, bucket sort, radix sort, comparison of algorithms, code, … Sorting: part 2.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
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 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
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 To understand quick-sort, let’s look at a high-level description of the algorithm 1) Divide : If the sequence S has 2 or more elements, select.
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.
Divide and Conquer Approach
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
Merge Sort 2/23/ :15 PM Merge Sort 7 2   7  2   4  4 9
Merge Sort 2/24/2019 6:07 PM Merge Sort 7 2   7  2  2 7
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Copyright © Aiman Hanna All rights reserved
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
Sorting And Searching CSE116A,B 4/7/2019 B.Ramamurthy.
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
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Merge Sort 5/30/2019 7:52 AM Merge Sort 7 2   7  2  2 7
CSCE 3110 Data Structures & Algorithm Analysis
CS203 Lecture 15.
Divide and Conquer Approach
CMPT 225 Lecture 10 – Merge Sort.
Chapter 11 Sets, and Selection
Presentation transcript:

1 More Merge Sort Java implementation Time complexity Generic merging and sets

2 Java Implementation of Merge-Sort

3 Java Implementation of Merge-Sort(cont.) public class ListMergeSort implements SortObject { public void sort(Sequence S, Comparator c) { int n = S.size(); if (n < 2) return; // a sequence with 0 or 1 element is already sorted. // divide Sequence S1 = (Sequence)S.newContainer(); // put the first half of S into S1 for (int i=1; i <= (n+1)/2; i++) { S1.insertLast(S.remove(S.first())); } Sequence S2 = (Sequence)S.newContainer(); // put the second half of S into S2 for (int i=1; i <= n/2; i++) { S2.insertLast(S.remove(S.first())); } sort(S1,c); // recur sort(S2,c); merge(S1,S2,c,S); // conquer }

4 Java Implementation of Merge Sort(cont.) public void merge(Sequence S1, Sequence S2, Comparator c, Sequence S) { while(!S1.isEmpty() && !S2.isEmpty()) { if(c.isLessThanOrEqualTo(S1.first().element(), S2.first().element())) { // S1’s 1st elt <= S2’s 1st elt S.insertLast(S1.remove(S1.first())); }else { // S2’s 1st elt is the smaller one S.insertLast(S2.remove(S2.first())); } }if(S1.isEmpty()) { while(!S2.isEmpty()) { S.insertLast(S2.remove(S2.first())); } }if(S2.isEmpty()) { while(!S1.isEmpty()) { S.insertLast(S1.remove(S1.first())); }}}

5 Running Time of Merge-Sort Proposition 1: The merge-sort tree associated with the execution of a merge-sort on a sequence of n elements has a height of  log n  Proposition 2: A merge sort algorithm sorts a sequence of size n in O(nlog n) time We assume only that the input sequence S and each of the sub- sequences created by each recursive call of the algorithm can access, insert to, and delete from the first and last nodes in O(1) time. We call the time spent at node v of merge-sort tree T the running time of the recusive call associated with v, excluding the recursive calls sent to v’s children.

6 Running Time of Merge-Sort (cont.) If we let i represent the depth of node v in the merge-sort tree, the time spent at node v is O(n/2 i ) since the size of the sequence associated with v is n/ 2 i. Observe that T has exactly 2 i nodes at depth i. The total time spent at depth i in the tree is then O (2 i n/ 2 i ), which is O(n). We know the tree has height  log n  Therefore, the time complexity is O(nlog n)

7 Set ADT A Set is a data structure modeled after the mathematical notation of a set. The fundamaental set operations are union, intersection, and subtraction. A brief aside on mathemeatical set notation: A  B={ x: x  A or x  B } A  B={ x: x  A and x  B } A-B={ x: x  A and x  B } The specific methods for a Set A include the following: union(B):Set A equal to A  B. intersect(B):Set A equal to A  B. subtract(B):Set A equal to A - B.

8 Generic Merging Algorithm genericMerge(A, B): Input: Sorted sequences A and B Output: Sorted sequence C let A’ be a copy of A { We won’t destroy A and B} let B’ be a copy of B while A’ and B’ are not empty doa  A’.first() b  B’.first() if a<b thena IsLess(a, C) A’.removeFirst() else if a=b thenbothAreEqual(a, b, C) A’.removeFirst() B’.removeFirst() elsebIsLess(b, C) B’.removeFirst() while A’ is not empty do a  A’.first() aIsLess(a, C) A’.removeFirst() while B’ is not empty do b  B’.first() bIsLess(b, C) B’.removeFirst()

9 Set Operations We can specialize the generic merge algorithm to perform set operations like union, intersection, and subtraction. The generic merge algorithm examines and compare the current elements of A and B. Based upon the outcome of the comparision, it determines if it should copy one or none of the elements a and b into C. This decision is based upon the particular operation we are performing, i.e. union, intersection or subtraction. For example, if our operation is union, we copy the smaller of a and b to C and if a=b then it copies either one (say a). We define our copy actions in aIsLess, bothAreEqual, and bIsLess. Let’s see how this is done...

10 Set Operations (cont.) public class UnionMerger extends Merger { protected void aIsLess(Object a, Object b, Sequence C) { C.insertLast(a); } protected void bothAreEqual(Object a, Object b,Sequence C) { C.insertLast(a); } protected void bIsLess(Object b, Sequence C) { C.insertLast(b); } For intersect public class IntersectMerger extends Merger { protected void aIsLess(Object a, Object b, SequenceC) { } protected void bothAreEqual(Object a, Object b, Sequence C) { C.insertLast(a); } protected void bIsLess(Object b, Sequence C) { } }

11 Set Operations (cont.) public class SubtractMerger extends Merger { protected void aIsLess(Object a, Object b, Sequence C) { C.insertLast(a); } protected void bothAreEqual(Object a, Object b, Sequence C) { } protected void bIsLess(Object b, Sequence C) { }