Sorting and selection – Part 2 Prof. Noah Snavely CS1114

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
CSE 3101: Introduction to the Design and Analysis of Algorithms
CSC 213 – Large Scale Programming or. Today’s Goals  Begin by discussing basic approach of quick sort  Divide-and-conquer used, but how does this help?
Sorting and Selection, Part 1 Prof. Noah Snavely CS1114
Introduction to Algorithms
Introduction to Algorithms Jiafen Liu Sept
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
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
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CS 206 Introduction to Computer Science II 04 / 28 / 2009 Instructor: Michael Eckmann.
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Chapter 7: Sorting Algorithms
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.
Quicksort Divide-and-Conquer. Quicksort Algorithm Given an array S of n elements (e.g., integers): If array only contains one element, return it. Else.
Quick Sort. Quicksort Quicksort is a well-known sorting algorithm developed by C. A. R. Hoare. The quick sort is an in-place, divide- and-conquer, massively.
CS 171: Introduction to Computer Science II Quicksort.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
CS 280 Data Structures Professor John Peterson. Project Not a work day but I’ll answer questions as long as they keep coming! I’ll try to leave the last.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CS 280 Data Structures Professor John Peterson. Project Questions?
Quicksort.
CS 1114: Sorting and selection (part one) Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Unit 061 Quick Sort csc326 Information Structures 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
Robustness and speed Prof. Noah Snavely CS1114
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
CS 206 Introduction to Computer Science II 12 / 08 / 2008 Instructor: Michael Eckmann.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
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.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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.
Robustness and speed Prof. Noah Snavely CS1114
Computer Science 101 A Survey of Computer Science QuickSort.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
QuickSort Choosing a Good Pivot Design and Analysis of Algorithms I.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Quicksort Quicksort is a well-known sorting algorithm that, in the worst case, it makes Θ(n 2 ) comparisons. Typically, quicksort is significantly faster.
Quicksort This is probably the most popular sorting algorithm. It was invented by the English Scientist C.A.R. Hoare It is popular because it works well.
Mergesort and Quicksort Opening Discussion zWhat did we talk about last class? zDo you have any questions about assignment #4? Have you thought.
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.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Chapter 7 Sorting Spring 14
CS 1114: Sorting and selection (part three)
Quick Sort (11.2) CSE 2011 Winter November 2018.
“Human Sorting” It’s a “Problem Solving” game:
slides adapted from Marty Stepp
Quickselect Prof. Noah Snavely CS1114
CS 1114: Sorting and selection (part two)
Data Structures & Algorithms
Quicksort and selection
“Human Sorting” It’s a “Problem Solving” game:
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

Sorting and selection – Part 2 Prof. Noah Snavely CS1114

2 Administrivia  Assignment 1 due tomorrow by 5pm  Assignment 2 will be out tomorrow –Two parts: smaller part due next Friday, larger part due in two weeks  Quiz next Thursday

Neat CS talk today  Culturomics: Quantitative Analysis of Culture Using Millions of Digitized Books  Upson B-17 4:15pm 3

Recap from last time  How can we quickly compute the median / trimmed mean of an array? –The selection problem  One idea: sort the array first –This makes the selection problem easier  How do we sort? 4

5 Recap from last time  Last time we looked at one sorting algorithm, selection sort  How fast is selection sort?

Speed of selection sort  Total number of comparisons: n + (n – 1) + (n – 2) + … + 1 6

Is this the best we can do?  Maybe the problem of sorting n numbers is intrinsically O(n 2 ) –(i.e., maybe all possible algorithms for sorting n numbers are O(n 2 ))  Or maybe we just haven’t found the right algorithm…  Let’s try a different approach –Back to the problem of sorting the actors… 7

Sorting, 2 nd attempt  Suppose we tell all the actors –shorter than 5.5 feet to move to the left side of the room  and all actors –taller than 5.5 feet to move to the right side of the room –(actors who are exactly 5.5 feet move to the middle) 8 [ ] [ ]

Sorting, 2 nd attempt  Not quite done, but it’s a start  We’ve put every element on the correct side of 5.5 (the pivot)  What next?  Divide and conquer 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) 10

11 Quicksort This algorithm is called quicksort 1. Pick an element (pivot) 2. Partition the array into elements pivot 3. Quicksort these smaller arrays separately  Example of a recursive algorithm (defined in terms of itself)

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

Quicksort – pseudo-code function [ S ] = quicksort(A) % Sort an array using quicksort n = length(A); if n <= 1 S = A; return; % The base case 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 where the recursion happens S = [ smaller equal larger ]; 13

14 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?  When can the bad case easily happen? 15

16 Quicksort and the pivot  With a good choice of pivot the algorithm does quite well  Suppose we get lucky and choose the median every time  How many comparisons will we do? –Every time quicksort is called, we have to: % Compare all elements to the pivot

How many comparisons? (Lucky pivot 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 the pivot (total # comparisons = ?) … now break each half into halves …  Round 3: For each quarter, compare n / 4 elements to the pivot (total # comparisons = ?) 17

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

How many comparisons? (Lucky pivot case)  During each round, we do a total of __ comparisons  There are ________ rounds  The total number of comparisons is _________  With “lucky pivots” quicksort is O(_________) 19

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

Questions? 21

Recursion 22  Recursion is cool and useful –Sierpinski triangle  But use with caution function x = factorial(n) x = n * factorial(n - 1) end

23 Back to the selection problem  Can solve with quicksort –Faster (on average) than “repeated remove biggest ”  Is there a better way?  Rev. Charles L. Dodgson’s problem –Based on how to run a tennis tournament –Specifically, how to award 2 nd prize fairly

24 How many teams were in the tournament? How many games were played? Which is the second-best team?