MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

Similar documents
LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Quicksort. The divide-and-conquer strategy is used in quicksort. Below the recursion step is described:

Sorting Algorithms. + Analysis of the Sorting Algorithms

DATA STRUCTURES AND ALGORITHMS

ECE 242 Data Structures and Algorithms. Advanced Sorting II. Lecture 17. Prof.

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

Back to Sorting More efficient sorting algorithms

Faster Sorting Methods

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

Better sorting algorithms (Weiss chapter )

KF5008 Algorithm Efficiency; Sorting and Searching Algorithms;

Merge Sort. Algorithm Analysis. November 15, 2017 Hassan Khosravi / Geoffrey Tien 1

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

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

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

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

Chapter 5. Quicksort. Copyright Oliver Serang, 2018 University of Montana Department of Computer Science

Programming II (CS300)

CS 171: Introduction to Computer Science II. Quicksort

Key question: how do we pick a good pivot (and what makes a good pivot in the first place)?

Divide and Conquer Algorithms: Advanced Sorting

Divide and Conquer Sorting Algorithms and Noncomparison-based

Unit Outline. Comparing Sorting Algorithms Heapsort Mergesort Quicksort More Comparisons Complexity of Sorting 2 / 33

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

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Outline. Quadratic-Time Sorting. Linearithmic-Time Sorting. Conclusion. Bubble/Shaker Sort Insertion Sort Odd-Even Sort

About this exam review

cmpt-225 Sorting Part two

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

Measuring algorithm efficiency

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

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

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

We can use a max-heap to sort data.

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CSC 273 Data Structures

17/05/2018. Outline. Outline. Divide and Conquer. Control Abstraction for Divide &Conquer. Outline. Module 2: Divide and Conquer

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

Assignment 4: Question 1b omitted. Assignment 5: Question 1b clarification

Lecture Notes on Quicksort

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

Topic 17 Fast Sorting

Topics Recursive Sorting Algorithms Divide and Conquer technique An O(NlogN) Sorting Alg. using a Heap making use of the heap properties STL Sorting F

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.

Design and Analysis of Algorithms Assignment-2 Solutions (Hints) REMARK: Quicksort

Sorting. Lecture10: Sorting II. Sorting Algorithms. Performance of Sorting Algorithms

CS 310 Advanced Data Structures and Algorithms

Sorting. Order in the court! sorting 1

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

"Organizing is what you do before you do something, so that when you do it, it is not all mixed up." ~ A. A. Milne SORTING

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

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

Sorting. Weiss chapter , 8.6

CS 171: Introduction to Computer Science II. Quicksort

CSE 373 NOVEMBER 8 TH COMPARISON SORTS

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

Lecture #2. 1 Overview. 2 Worst-Case Analysis vs. Average Case Analysis. 3 Divide-and-Conquer Design Paradigm. 4 Quicksort. 4.

Chapter 7 Sorting. Terminology. Selection Sort

SORTING. Comparison of Quadratic Sorts

Programming II (CS300)

CIS 121 Data Structures and Algorithms with Java Spring Code Snippets and Recurrences Monday, January 29/Tuesday, January 30

CSCI 136 Data Structures & Advanced Programming. Lecture 14 Spring 2018 Profs Bill & Jon

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

SORTING. Insertion sort Selection sort Quicksort Mergesort And their asymptotic time complexity

CSE 373: Data Structures and Algorithms

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

// walk through array stepping by step amount, moving elements down for (i = unsorted; i >= step && item < a[i-step]; i-=step) { a[i] = a[i-step];

Cpt S 122 Data Structures. Sorting

Quick Sort. CSE Data Structures May 15, 2002

Quicksort (Weiss chapter 8.6)

CS125 : Introduction to Computer Science. Lecture Notes #40 Advanced Sorting Analysis. c 2005, 2004 Jason Zych

ECE 242 Data Structures and Algorithms. Advanced Sorting I. Lecture 16. Prof.

Plan of the lecture. Quick-Sort. Partition of lists (or using extra workspace) Quick-Sort ( 10.2) Quick-Sort Tree. Partitioning arrays

Sorting. Order in the court! sorting 1

Lecture Notes on Quicksort

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

Sorting. CSC 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation CSC Picture

COMP Data Structures

Unit-2 Divide and conquer 2016

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

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

CSE 373: Data Structures and Algorithms

Cosc 241 Programming and Problem Solving Lecture 17 (30/4/18) Quicksort

ECE368 Exam 2 Spring 2016

algorithm evaluation Performance

Overview of Sorting Algorithms

Sorting. Bringing Order to the World

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

You must bring your ID to the exam.

CS 445: Data Structures Final Examination: Study Guide

CSE 373: Data Structures and Algorithms

BM267 - Introduction to Data Structures

COSC 311: ALGORITHMS HW1: SORTING

Sort: Divide & Conquer. Data Structures and Algorithms Emory University Jinho D. Choi

CS 61B Summer 2005 (Porter) Midterm 2 July 21, SOLUTIONS. Do not open until told to begin

Sorting Algorithms Day 2 4/5/17

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

InserDonSort. InserDonSort. SelecDonSort. MergeSort. Divide & Conquer? 9/27/12

CS302 - Data Structures using C++

Transcription:

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015 1

administrivia 2

-assignment 4 due tonight at midnight -assignment 5 is out -midterm next Tuesday 3

last time 4

re cur sion [ri-kur-zhuh n] noun see recursion. 5

-recursion is a problem solving technique in which the solution is defined in terms of a simpler (or smaller) version of the problem -break the problem into smaller parts -solve the smaller problems -combine the results -a recursive method calls itself -some functions are easiest to define recursively sum(n) = sum(n-1) + N -there must be at least on base case that can be computed without recursion -any recursive call must make progress towards the base case! 6

a simple example sum(n) = sum(n-1) + N public static int sum(int n) { } if(n == 1) return 1; return sum(n-1) + n; fix to handle zero or negative values... how can we solve the same problem without recursion? which is better, the recursive solution or the alternative? 7

-recursive methods often have unusual parameters -at the top level, we just want: binarysearch(arr, item); -but in reality, we have to call: binarysearch(arr, item, 0, arr.length-1); -driver methods are wrappers for calling recursive methods -driver makes the initial call to the recursive method, knowing what parameters to use -is not recursive itself public static boolean binarysearch(arr, item){ return binarysearchrecursive( arr, item, 0, arr.length-1); } 8

recursion, beware -do not use recursion when a simple loop will do -growth rates may be the same, but - there is a lot of overhead involved in setting up the method frame -way more overhead than one iteration of a for-loop -do not do redundant work in a recursive method -move validity checks to a driver method -too many recursive calls will overflow the call stack -stack stores state from all preceding calls 9

4 recursion rules 1. always have at least one case that can be solved without using recursion 2. any recursive call must progress toward a base case 3. always assume that the recursive call works, and use this assumption to design your algorithms 4. never duplicate work by solving the same instance of a problem in separate recursive calls 10

today 11

-mergesort -quicksort -midterm stuff 12

mergesort divide and conquer 13

but first, merging -say we have two sorted lists, how can we efficiently merge them? -idea: compare the first element in each list to each other, take the smallest and add to the merged list -AND repeat what does this look like? 14

mergesort 1)divide the array in half 2)sort the left half 3)sort the right half how do we sort? can we avoid sorting? how? 4)merge the two halves together what is missing here? 15

mergesort 1)divide the array in half 2)sort the left half 2) take the left half, and go back to step 1 until??? 3)sort the right half 4)merge the two halves together 3) take the right half, and go back to step 1 until??? what does this look like? 16

void mergesort(int[] arr, int left, int right) { int mid = (left + right) / 2; mergsort(arr, left, mid); divide mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); } conquer what are we missing? 17

void mergesort(int[] arr, int left, int right) { // arrays of size 1 are already sorted if(start >= end) return; } int mid = (left + right) / 2; mergsort(arr, left, mid); mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); divide conquer what is the complexity of the divide step? A) c B) log N C) N D) N log N E) N 2 F) N 3 18

void mergesort(int[] arr, int left, int right) { // arrays of size 1 are already sorted if(start >= end) return; } int mid = (left + right) / 2; mergsort(arr, left, mid); mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); divide conquer what is the complexity of the conquer step? A) c B) log N C) N D) N log N E) N 2 F) N 3 19

void mergesort(int[] arr, int left, int right) { // arrays of size 1 are already sorted if(start >= end) return; } int mid = (left + right) / 2; mergsort(arr, left, mid); mergsort(arr, mid+1, right); merge(arr, left, mid+1, right); divide conquer what is the complexity of MERGESORT? A) c B) log N C) N D) N log N E) N 2 F) N 3 is this the worst average best-case? 20

merging sorted arrays -easy concept, tricky code -lots of special cases: -keep track of two indices to step through both arrays (the front of each array) -indices do not necessarily move at the same speed -have to stop the loop when either index reaches the end of their array -the two arrays are not necessarily the same size -what to do when you reach the end of one array but not the other? -copy from temp back into the array 21

void Merge(int[] arr, start, mid, end) { // create temp array for holding merged arr int[] temp = new int[end start + 1]; int i1 = 0, i2 = mid; while(i1 < mid && i2 < end) { put smaller of arr[i1], arr[i2] into temp; } } copy anything left over from larger half to temp; copy temp over to arr; Is there anything we should do different? 22

allocate Temp in driver method! void Merge(int[] arr, int[] temp, start, mid, end) { int i1 = 0, i2 = mid; while(i1 < mid && i2 < end) { put smaller of arr[i1], arr[i2] into temp; } } copy anything left over from larger half to temp; copy temp over to arr; Is there anything we should do different? 23

notes on merging -the major disadvantage of mergesort is that the merging of two arrays requires an extra, temporary array -this means that mergesort requires 2x as much space as the array itself -can be an issue if space is limited! -an in-place mergesort exists, but is complicated and has worse performance -to achieve the overall running time of O(N log N) it is critical that the running time of the merge phase be linear 24

mergesort variation -it is a good idea to invoke insertion sort when the subarray size reaches a small enough threshold -why??? -HINT: what is the complexity of insertion sort? of mergesort? what are other runtime considerations? -the real threshold depends on several things: -hardware / OS / compiler -input characteristics 25

quicksort another divide and conquer 26

quicksort 1)select an item in the array to be the pivot 2)partition the array so that all items less than the pivot are to the left of the pivot, and all the items greater than the pivot are to the right 3)sort the left half 4)sort the right half NOTE: after partitioning, the pivot is in it s final position! what do you notice? 27

quicksort 1)select an item in the array to be the pivot 2)partition the array so that all items less than the pivot are to the left of the pivot, and all the items greater than the pivot are to the right 3)sort the left half 4)sort the right half 3) take the left half, and go back to step 1 until??? 4) take the right half, and go back to step 1 until??? what does this look like? 28

void quicksort(int[] arr, int left, int right) { int pivot_index = partition(arr, left, right); quicksort(arr, left, pivot_index-1); quicksort(arr, pivot_index+1, right); } what are we missing? 29

void quicksort(int[] arr, int left, int right) { // arrays of size 1 are already sorted if(start >= end) return; } int pivot_index = partition(arr, left, right); quicksort(arr, left, pivot_index-1); quicksort(arr, pivot_index+1, right); what is the divide step? what is the conquer step? 30

quick note -a huge benefit of quicksort is that it can be done in-place -ie. you can do the sort within the original array -mergesort requires an extra, temporary array for merging -however, in-place partitioning for quicksort requires some careful thought 31

in-place partitioning 1)select an item in the array to be the pivot 2)swap the pivot with the last item in the array (just get it out of the way) 3)step from left to right until we find an item > pivot -this item needs to be on the right of the partition 4)step from right to left until we find an item < pivot -this item needs to be on the left of the partition 5)swap items 6)continue until left and right stepping cross 7)swap pivot with left stepping item what does this look like? 32

find pivot, swap with right_bound; L = left_bound, R = right_bound - 1; while(l <= R) { if(arr[l] <= pivot) { L++; continue; // find next item > pivot } if(arr[r] >= pivot) { R--; continue; // find its swapping partner } } swap(arr, L, R); // partners found, swap them L++; R--; // point where L met R is the pivot location swap(arr, L, right_bound); // put pivot back 33

choosing a pivot -the median of all array items is the best possible choice why? -is time-consuming to compute -finding true median is O(N) -it is important that we avoid the worst case -what IS the worst case(s)? -middle array item is a safe choice why? -median-of-three: pick a few random items and take median -why not the first, middle, and last items? -random pivot: faster than median-of-three, but lower quality 34

choosing a pivot -any nonrandom pivot selection has some devious input that causes O(N 2 ) -trade-off between quality of pivot and time to select -selection cost should always be O(c) -ie. it should not depend on N! 35

quicksort complexity -performance of quick sort heavily depends on which array item is chosen as the pivot -best case: pivot partions the array into two equallysized subarrays at each stage O(N log N) -worst case: partition generates an empty subarray at each stage O(N 2 ) -average case: bound is O(N log N) -proof is quite involved, see the textbook if you are curious 36

quicksort vs mergesort 37

-both are O(N log N) in the average case -mergesort is also O(N log N) in the worst case -so, why not always use mergesort? -mergesort requires 2N space -and, copying everything from the merged array back to the original takes time -quicksort requires no extra space -thus, no copying overhead! -but, in O(N 2 ) worst case <wha wha> 38

-both are divide and conquer algorithms (recursive) -mergesort sorts on the way up -after the base case is reached, sorting is done as the calls return and merge -quicksort sorts on the way down -once the base case is reached, that part of the array is sorted -though quicksort is more popular, it is not always the right choice! 39

sorting summary a Selection Sort Best Average Worst Notes O(N 2 ) O(N 2 ) O(N 2 ) Never used in practice Insertion Sort O(N) O(N 2 ) O(N 2 ) Shellsort O(N log N) O(N 1.25 ) O(N 1.5 )! Mergesort O(N log N) O(N log N) O(N log N) Quicksort O(N log N) O(N log N) O(N^2) 40 Takes advantage of sortedness Depends on gap sizes 2x space overhead, guaranteed O(N log N) Depends on pivot

midterm 41

topics -Java basics -variables, types -control flow -reference types -classes, methods -OOP -inheritance -polymorphism -interfaces -super 42

topics -generics -wild cards -generic classes -comparators -algorithm analysis -growth rates -Big-O -determining complexity of loops and algorithms 43

topics -Collections -Iterators -recursion -selection sort -insertion sort -shellsort -mergesort -quicksort be able to reason about performance and behavior of each! 44

test format -problems may be of the following types: -short answer -determining output of code -writing code -filling in missing code -multiple choice -true / false 45

next time 46

-no lab on Monday -midterm on Tuesday in class -reading -chapter 17 -chapter 3 -http://opendatastructures.org/ods-java/ -homework -assignment 5 due Thursday 47