CS 1114: Sorting and selection (part two)

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Sorting and Selection, Part 1 Prof. Noah Snavely CS1114
QuickSort 4 February QuickSort(S) Fast divide and conquer algorithm first discovered by C. A. R. Hoare in If the number of elements in.
Quicksort CSE 331 Section 2 James Daly. Review: Merge Sort Basic idea: split the list into two parts, sort both parts, then merge the two lists
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Quicksort.
CS 1114: Sorting and selection (part one) Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
Quicksort
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
Quick Sort By: HMA. RECAP: Divide and Conquer Algorithms This term refers to recursive problem-solving strategies in which 2 cases are identified: A case.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Order Statistics David Kauchak cs302 Spring 2012.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Sorting.
Quick Sort Divide: Partition the array into two sub-arrays
Quicksort
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
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,
Divide-And-Conquer-And-Combine
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
Quicksort "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Advance Analysis of Algorithms
CSE 143 Lecture 23: quick sort.
Quicksort 1.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Chapter 4: Divide and Conquer
CS 1114: Sorting and selection (part three)
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Unit-2 Divide and Conquer
Data Structures Review Session
8/04/2009 Many thanks to David Sun for some of the included slides!
Divide-And-Conquer-And-Combine
Sorting Chapter 8 CS 225.
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Quickselect Prof. Noah Snavely CS1114
CSE 326: Data Structures Sorting
EE 312 Software Design and Implementation I
CS 3343: Analysis of Algorithms
Quicksort.
CSE 373 Data Structures and Algorithms
Ch. 2: Getting Started.
CSC 143 Java Sorting.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Topic: Divide and Conquer
Data Structures & Algorithms
Quicksort.
The Selection Problem.
Quicksort and Randomized Algs
Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
Quicksort and selection
Quicksort.
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

CS 1114: Sorting and selection (part two) Prof. Graeme Bailey http://cs1114.cs.cornell.edu (notes modified from Noah Snavely, Spring 2009)

Recap from last time We looked at the “trimmed mean” problem for locating the lightstick Remove 5% of points on all sides, find centroid This is a version of a more general problem: Finding the kth largest element in an array Also called the “selection” problem To solve this, we try first solving the sorting problem (then getting the kth largest is easy) How fast is this algorithm?

Is this the best we can do? Let’s try a different approach Suppose we tell all the actors shorter than 5.5’ to move to the left side of the room and all actors taller than 5.5’ to move to the right side of the room (actors who are exactly 5.5’ move to the middle) [ 6.0 5.4 5.5 6.2 5.3 5.0 5.9 ] [ 5.4 5.3 5.0 5.5 6.0 6.2 5.9 ]

Sorting, 2nd attempt [ 6.0 5.4 5.5 6.2 5.3 5.0 5.9 ] Not quite done, but it’s a start We’ve put every element on the correct side of 5.5’ (the pivot) What next? Do this again on each side: ask shorter group to pivot on (say) 5.3’, taller group to pivot on 6.0’ Divide and conquer [ 5.4 5.3 5.0 5.5 6.0 6.2 5.9 ] < 5.5’ > 5.5’

How do we select the pivot? How did we know to select 5.5’ as the pivot? Answer: average-ish human height In general, we might not know a good value Solution: just pick some value from the array (say, the first one)

Quicksort This algorithm is called quicksort Pick an element (pivot) Compare every element to the pivot and partition the array into elements < pivot and > pivot Quicksort these smaller arrays separately

Quicksort example [ 10 13 41 6 51 11 3 ] 10 [ 6 3 10 13 41 51 11 ] Select pivot [ 10 13 41 6 51 11 3 ] 10 Partition [ 6 3 10 13 41 51 11 ] Select pivot [ 6 3 ] 10 [ 13 41 51 11 ] 6 13 Partition [ 3 6 ] 10 [ 11 13 41 51 ] Select pivot [ 3 ] 6 10 [ 11 ] 13 [ 41 51 ] 41 Partition 3 6 10 11 13 [ 41 51 ] Select pivot 3 6 10 11 13 41 [ 51 ] Done 3 6 10 11 13 41 51

Quicksort – pseudo-code function [ S ] = quicksort(A) % Sort an array using quicksort n = length(A); if n <= 1 S = A; return; end pivot = A(1); % Choose the pivot smaller = []; equal = []; larger = []; % Compare all elements to the pivot: % Add all elements smaller than pivot to ‘smaller’ % Add all elements equal to pivot to ‘equal’ % Add all elements larger than pivot to ‘larger’ % Sort ‘smaller’ and ‘larger’ separately smaller = quicksort(smaller); larger = quicksort(larger); % This is called recursion S = [ smaller equal larger ];

Quicksort and the pivot There are lots of ways to make quicksort fast, for example by swapping elements We will cover these in section

Quicksort and the pivot With a bad pivot this algorithm does quite poorly Suppose we happen to always pick the smallest element of the array? What does this remind you of? Number of comparisons will be O(n2) When can the bad case easily happen? The worst case occurs when the array is already sorted We could choose the average element instead of the first element

Quicksort: best case With a good choice of pivot the algorithm does quite well What is the best possible case? Selecting the median (how easy is this to find?) How many comparisons will we do? Every time quicksort is called, we have to: % Compare all elements to the pivot

How many comparisons? (best case) Suppose length(A) == n Round 1: Compare n elements to the pivot … now break the array in half, quicksort the two halves … Round 2: For each half, compare n / 2 elements to each pivot (total # comparisons = n) … now break each half into halves … Round 3: For each quarter, compare n / 4 elements to each pivot (total # comparisons = n)

How many comparisons? (best case) … How many rounds will this run for?

How many comparisons? (best case) During each round, we do a total of n comparisons There are log n rounds The total number of comparisons is n log n In the best case quicksort is O(n log n)

Can we expect to be lucky? Performance depends on the input “Unlucky pivots” (worst-case) give O(n2) performance “Lucky pivots” give O(n log n) performance For random inputs we get “lucky enough” – expected runtime on a random array is O(n log n) Can we do better?

Another sort of sort (step 1, decompose) Suppose length(A) == n Round 1: Break the array in half Round 2: Now break each half in half … Round log(n): Now have a lot (n) of small arrays!

Another sort of sort (step 2, recombine) It’s easy to sort an array of length one! So … Round 1: Merge them pairwise in the correct order Round 2: Now merge these… Round log(n): Now we have one sorted array!

Back to the selection problem Can solve with sorting Is there a better way? Rev. Charles L. Dodgson’s problem Based on how to run a tennis tournament Specifically, how to award 2nd prize fairly We’ll dodge this until next time ……..