Do you remember any iterative sorting methods? Can we produce a good sorting method by. We try to divide and conquer: break into subproblems

Similar documents
Fundamental problem in computing science. putting a collection of items in order. Often used as part of another algorithm

n 1 x i = xn 1 x 1. i= (2n 1) = n 2.

Lecture 6: Sequential Sorting

Back to Sorting More efficient sorting algorithms

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

(Refer Slide Time: 01.26)

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Mergesort again. 1. Split the list into two equal parts

COMP Data Structures

Unit-2 Divide and conquer 2016

SORTING AND SELECTION

Sorting Algorithms. + Analysis of the Sorting Algorithms

Divide and Conquer Algorithms

Searching and Sorting

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

Sorting Pearson Education, Inc. All rights reserved.

Lecture Notes on Quicksort

The divide and conquer strategy has three basic parts. For a given problem of size n,

Lecture 7 Quicksort : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CS 506, Sect 002 Homework 5 Dr. David Nassimi Foundations of CS Due: Week 11, Mon. Apr. 7 Spring 2014

CmpSci 187: Programming with Data Structures Spring 2015

CPSC W2 Midterm #2 Sample Solutions

CSE373: Data Structure & Algorithms Lecture 21: More Comparison Sorting. Aaron Bauer Winter 2014

Algorithms - Ch2 Sorting

Greedy algorithms is another useful way for solving optimization problems.

Better sorting algorithms (Weiss chapter )

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Computer Science 236 Fall Nov. 11, 2010

Test 1 Review Questions with Solutions

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Lecture Notes 14 More sorting CSS Data Structures and Object-Oriented Programming Professor Clark F. Olson

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

Quicksort (Weiss chapter 8.6)

CSE 373: Data Structures and Algorithms

Problem. Input: An array A = (A[1],..., A[n]) with length n. Output: a permutation A of A, that is sorted: A [i] A [j] for all. 1 i j n.

Section 7.3 Divide-and-Conquer Algorithms and Recurrence Relations. 0,a m. - Division into thirds gives m = 3

Lecture Notes on Quicksort

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

Sorting. There exist sorting algorithms which have shown to be more efficient in practice.

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Computer Science 385 Analysis of Algorithms Siena College Spring Topic Notes: Divide and Conquer

S O R T I N G Sorting a list of elements implemented as an array. In all algorithms of this handout the sorting of elements is in ascending order

Unit #3: Recursion, Induction, and Loop Invariants

Figure 4.1: The evolution of a rooted tree.

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

SORTING, SETS, AND SELECTION

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Chapter 3:- Divide and Conquer. Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Introduction to Computers and Programming. Today

Randomized Algorithms: Selection

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

Sorting. Weiss chapter , 8.6

CS102 Sorting - Part 2

Sorting is a problem for which we can prove a non-trivial lower bound.

Recursion Chapter 3.5

Hi everyone. Starting this week I'm going to make a couple tweaks to how section is run. The first thing is that I'm going to go over all the slides

S1) It's another form of peak finder problem that we discussed in class, We exploit the idea used in binary search.

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Lecture 15 : Review DRAFT

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Fun facts about recursion

Topic 17 Fast Sorting

EECS 2011M: Fundamentals of Data Structures

Lecture 7: Searching and Sorting Algorithms

Sorting & Growth of Functions

Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

08 A: Sorting III. CS1102S: Data Structures and Algorithms. Martin Henz. March 10, Generated on Tuesday 9 th March, 2010, 09:58

CS S-11 Sorting in Θ(nlgn) 1. Base Case: A list of length 1 or length 0 is already sorted. Recursive Case:

COSC242 Lecture 7 Mergesort and Quicksort

Week - 03 Lecture - 18 Recursion. For the last lecture of this week, we will look at recursive functions. (Refer Slide Time: 00:05)

Design and Analysis of Algorithms

CS126 Final Exam Review

Divide and Conquer Algorithms: Advanced Sorting

Sorting: Given a list A with n elements possessing a total order, return a list with the same elements in non-decreasing order.

Divide and Conquer Sorting Algorithms and Noncomparison-based

Data structures. More sorting. Dr. Alex Gerdes DIT961 - VT 2018

Scribe: Sam Keller (2015), Seth Hildick-Smith (2016), G. Valiant (2017) Date: January 25, 2017

CS 206 Introduction to Computer Science II

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Sorting. Task Description. Selection Sort. Should we worry about speed?

9. The Disorganized Handyman

6.001 Notes: Section 4.1

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Lecture 11: In-Place Sorting and Loop Invariants 10:00 AM, Feb 16, 2018

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

DATA STRUCTURES AND ALGORITHMS

12/30/2013 S. NALINI,AP/CSE

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Comparison Sorts. Chapter 9.4, 12.1, 12.2

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

Lecture 9: Sorting Algorithms

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

We can use a max-heap to sort data.

Lecture 2: Getting Started

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Transcription:

MERGESORT 315

Sorting Recursively Do you remember any iterative sorting methods? How fast are they? Can we produce a good sorting method by thinking recursively? We try to divide and conquer: break into subproblems and put their solutions together. 316

Divide and Conquer Let's try it on the following list of letters: P E N G U I N S Break it into two lists: P E N G U I N S Sort them: E G N P I N S U Is putting these two lists together easier than sorting the whole list? 317

Merging Combining two sorted lists into one sorted list is called merging. E G N P I N S U Starting at the front of both lists, we keep picking the smallest letter: *E* G N P I N S U G N P *I* N S U *G* N P N S U etc. 318

Code for Mergesort We'll implement it for arrays. We only care about the order of elements so we assume only that the elements are Comparable. Also, we'll try sorting the array `in place', rather than creating a new array to contain the result. /** Sort list[s..e] in non-decreasing order. */ public static void mergesort(comparable[] list, int s, int e) { } if (s < e) { // More than one element int mid = (s + e) / 2; mergesort(list, s, mid); mergesort(list, mid + 1, e);...merge list[s..m] and list[m+1..e] into list[s..e]... } // else just 1 element, so already sorted. 319

Except possibly for the merge portion, this was very simple to write. What can we do to understand how it works? if the recursive calls work, the method will clearly work trace it Exercise: Write a requires clause for the method. Exercise: Write a wrapper method for the common case of mergesorting an entire array. Exercise: Would mergesort still work if the two recursive calls were: mergesort(list, s, mid-1); mergesort(list, mid, e); 320

Header for Merge Let's make it a helper method. What header should it have? Imagine calling it from mergesort: merge(list, s, mid, e); This gets us started with the header. /** Merge list[s..m] with list[m+1..e]. * Requires: the two sublists are sorted * in non-decreasing order. * Ensures: list[s..e] is sorted. */ private static void merge(comparable[] list, int s, int m, int e) 321

Body of Merge int p1 = s; // index of current candidate in list[s..m] int p2 = m + 1; // index of current candidate // in list[m+1..e] // Temporary storage to accumulate the merged result. Comparable[] merged = new Comparable[e - s + 1]; int p = 0; // index to put next element into merged // merged[0..p] contains list[s..p1-1] merged // with list[m+1..p2-1] while (p1!= m + 1 && p2!= e + 1) { if (list[p1].compareto(list[p2]) < 0) { merged[p] = list[p1]; ++p1; } else { merged[p] = list[p2]; ++p2; } ++p; }... to be continued... What's true at the end of the while loop? What's left to do? 322

/* if (p1!= m + 1) { System.arraycopy(list, p1, list, s + p, m + 1 - p1); } System.arraycopy(merged, 0, list, s, p); */ Exercise: method. Finish the requires clause for the Exercise: If we had copied the remainder of the unnished sublist to merged and then copied all of merged back to list, would that have changed the big-o time complexity? 323

Exercise: Rewrite mergesort and merge to follow the Java convention for ranges: passing s and e as the start and end of a sublist of list means list[s..e) (i.e. list[s..e-1]). Exercise: Write a recursive merge method. Exercise: Write a mergesort for linked lists. Exercise: Does the following approach to an in-place merge work: If the next element is in the rst list, then leave it there and move forward in the rst list, otherwise swap it with the next element in the second list and move forward in both lists. 324

Eciency of Sorting Methods What sorting methods other than mergesort do you know? How ecient are they? Let's see how merge sort compares. With loops, we know how to analyze eciency. What to do with recursion? Strategy: Determine (1) the total number of calls to mergesort and (2) the time per call. Multiply them together. 325

(1) Number of Calls to mergesort Suppose we call mergesort with a list of 8 elements. What happens? Number of calls in total: 326

Other sizes of list Let C(n) be the total number of calls to mergesort if it is called initially with a list of n elements. We know C(8) = 15. Determine C(n) for some other values of n. To make it easier, consider only powers of 2. Results: n 1 2 4 8 16 : : : C(n) General rule: 327

Proving the rule We only derived the rule for powers of two, so the theorem should only make its claim for powers of 2: Theorem: for n 1 any power of 2, C(n) = 2n, 1: An equivalent theorem: Theorem: for all integers p 0, C(2 p ) = 2 2 p, 1. Let's prove it, by induction on p. 328

Prove: For all integers p 0, C(2 p ) = 2 2 p, 1. Base Case: Prove C(2 0 ) = 2 2 0, 1. Consider a call to mergesort with a list of length 2 0 = 1. The if condition fails and we immediately return. The total number of calls is thus just 1. Therefore C(2 0 ) = 1 which is the same as 2 2 0, 1. Let k be an arbitrary integer 0. Induction Hypothesis: Assume C(2 k ) = 2 2 k, 1. Induction Step: Prove C(2 k+1 ) = 2 2 k+1, 1. Consider a call to mergesort with a list of length 2 k+1. Since k 0, 2 k+1 2. So the if-condition succeeds, and we make two recursive calls. Each call involves 2 k+1 =2 = 2 k elements. By the induction hypothesis, C(2 k ) = 2 2 k, 1. So each recursive call causes a total of 2 2 k, 1 calls. The total number of calls, for an initial list of length 2 k+1 = 1 + 2(2 2 k, 1) = 1 + 2(2 k+1, 1) = 1 + 2 2 k+1, 2 = 2 2 k+1, 1 Thus C(2 k+1 ) = 2 2 k+1, 1. Conclusion: For all integers p 0, C(2 p ) = 2 2 p, 1. 329

(2) Time per call We now know the total number of calls made to mergesort. Let's gure out the time required per call so that we can put it all together. In big-o terms, how much time is required to execute a call to mergesort on a list of n items? Immediate problem: The size of the list is different every time we recurse. Hm. 330

Another way to analyze mergesort Do you notice anything about the rows in the tree of calls? ms(8) ms(4) ms(4) ms(2) ms(2) ms(2) ms(2) ms(1) ms(1) ms(1) ms(1) ms(1) ms(1) ms(1) ms(1) 331

Analysis of mergesort How much time is required to merge two sorted lists into one list with total size s? So how much time in total is required for all the merging in any single row? How many rows are there? So what is the total amount of time required? Exercise: What eect would it have on the time required for mergesort if we split the list into 3 pieces instead of 2? Exercise: How much time would a (good version of) mergesort take for linked lists? 332

Comparing with other sorts Many familiar sorts are O(n 2 ) in the worst case, including: bubble sort, selection sort, insertion sort Some other sorts are O(n log n) in the worst case (like mergesort), for example heap sort. But that's only part of the story. We also care about the average case. This is much harder to determine, because it requires arguing about the probability of seeing various cases. special cases, such as a list that is only slightly out of sorted order, is already sorted, or is sorted in the reverse order. 333

Analysis of Recursive Code Our analysis of mergesort required us to unwind the recursion. If you truly understand recursive code, you don't have to unwind it in order to understand or analyze it (but it's ok to unwind it). Recurrence relations are suited to analyzing eciency of recursive code: REC RELN CODE initial condition $ degenerate case general rule $ recursive case Induction is suited to proving correctness of recursive code: PROOF CODE base case $ degenerate case induction step $ recursive case 334