Quicksort and selection

Slides:



Advertisements
Similar presentations
Order of complexity. Consider four algorithms 1.The naïve way of adding the numbers up to n 2.The smart way of adding the numbers up to n 3.A binary search.
Advertisements

©2001 by Charles E. Leiserson Introduction to AlgorithmsDay 9 L6.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 6 Prof. Erik Demaine.
Chapter 9 continued: Quicksort
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Algorithms Bryce Boe 2012/08/13 CS32, Summer 2012 B.
Sorting and Selection, Part 1 Prof. Noah Snavely CS1114
Introduction to Algorithms Jiafen Liu Sept
Chapter 9: Searching, Sorting, and Algorithm Analysis
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
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.
Quicksort.
CS 1114: Sorting and selection (part one) Prof. Graeme Bailey (notes modified from Noah Snavely, Spring 2009)
Quicksort
Robustness and speed Prof. Noah Snavely CS1114
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.
Medians and blobs Prof. Ramin Zabih
Robustness and speed Prof. Noah Snavely CS1114
Sorting and selection – Part 2 Prof. Noah Snavely CS1114
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Algorithmic complexity: Speed of algorithms
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Prof. Ramin Zabih Implementing queues Prof. Ramin Zabih
Week 9 - Monday CS 113.
Week 13: Searching and Sorting
May 17th – Comparison Sorts
CMSC201 Computer Science I for Majors Lecture 23 – Sorting
Introduction to Search Algorithms
Last Class We Covered Sorting algorithms Searching algorithms
Computer Science 112 Fundamentals of Programming II
COMP 53 – Week Seven Big O Sorting.
Sorting Chapter 13 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and Insertionsort.
Quicksort
Randomized Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Lecture 8 Randomized Algorithms
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.
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
Week 12 - Wednesday CS221.
Quicksort 1.
CS 1114: Sorting and selection (part three)
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Quicksort analysis Bubble sort
Randomized Algorithms
Data Structures Review Session
Last Class We Covered Sorting algorithms Searching algorithms
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Sub-Quadratic Sorting Algorithms
Quickselect Prof. Noah Snavely CS1114
Simple Sorting Methods: Bubble, Selection, Insertion, Shell
Sorting "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.
CSE 326: Data Structures Sorting
Sorting, selecting and complexity
EE 312 Software Design and Implementation I
Quicksort.
CS 1114: Sorting and selection (part two)
Ch. 2: Getting Started.
CSC 143 Java Sorting.
Algorithms CSCI 235, Spring 2019 Lecture 20 Order Statistics II
Quicksort.
The Selection Problem.
Quicksort and Randomized Algs
Core Assessments Core #1: This Friday (5/4) Core #2: Tuesday, 5/8.
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
Quicksort.
Data Structures and Algorithms CS 244
Sorting and selection Prof. Noah Snavely CS1114
Presentation transcript:

Quicksort and selection Prof. Ramin Zabih http://cs100r.cs.cornell.edu

Administrivia Assignment 2 is out, due in 2 pieces Small amount is due this Friday Most of it is due next Friday Quiz 1 and Assignment 1 are graded You can check CMS Optional evening lecture(s) coming soon! “Kindergarten cop” will be in a month or so Quiz 2 on Tuesday 9/18 Coverage through the next lecture

Recap from last time Big-O notation allows us to reason about speed without worrying about Getting lucky on the input Depending on our hardware Repeatedly removing the biggest element allows us to find the kth largest Problem is called “selection” Algorithm is quadratic, O(n2)

How to do selection better? If our input were sorted, we can do better Given 100 numbers in increasing order, we can easily figure out the 5th biggest or smallest Very important principle! Divide your problem into pieces One person (or group) can provide sort The other person can use sort As long as both agree on what sort does, they can work independently Can even “upgrade” to a faster sort We will do this in CS100R

How to sort? Sorting is an ancient problem, by the standards of CS First important “computer” sort used for 1890 census, by Hollerith Strangely enough, we will talk about the census again shortly (the 1900 census) There are many algorithms We will focus on quicksort

Quicksort summary Pick an element (pivot) Partition the array into elements <= pivot and > pivot Quicksort these smaller arrays separately Example: [10 2 5 30 4 8 19] If the pivot is 8 we need to sort: [2 5 4 8] and [10 30 19] Sort these using quicksort

The selection problem Rev. Charles L. Dodgson’s problem Based on how to run a tennis tournament Specifically, how to award 2nd prize fairly

Quicksort and the pivot There are lots of ways to make quicksort fast, for example by swapping elements We will cover these in section Notice that with a bad choice of pivot this algorithm does quite poorly Suppose we happen to always pick the largest element of the array, or the smallest? What does this remind you of? When can the bad case easily happen?

Modifying quicksort to select Suppose that we want to find the 5th largest element (4 are larger) Quicksort kind of helps us do this already When we partition the array, we know the element we want is in one smaller array! So, we can figure out which smaller array we need to search For example, if we have 10 elements, 7 <= pivot and 3 > pivot We need to search for the 2nd largest element in the smaller array with 7 elements

Modified quicksort example Example: find 5th largest (< 4) in: [8 3 4 6 3 8 5 10 10 7] The ones bigger than 8 are: [ 10 10 ] So we need the 3rd largest (< 2) in: [8 3 4 6 3 8 5 7]

Find element < k others in A MODIFIED QUICKSORT: Pick an element in A as the pivot, call it x Divide A into A1 (<x), A2 (=x), A3 (>x) If k < length(A3) Find the element < k others in A3 If k > length(A2) + length(A3) Find the correct element in A1 Let j = k – [length(A2) + length(A3)] Find the element < j others in A1 Otherwise, return x

What is the complexity of: Finding the 1st element? O(1) (effort doesn’t depend on input) Finding the biggest element? O(n) (constant work per input element) Finding the median, by repeatedly finding and removing the biggest element? O(n2) (linear work per input element) Finding the median using modified quicksort?

 What is the complexity of: Finding the 2nd biggest element (>all but 1)? The 3rd biggest (> all but 2)? What do you think? It’s actually O(n) We do 2 (or 3) “find biggest” operations Each of which takes O(n) time Finding the element bigger than all but 5%? Assume we do this by repeated “find biggest” What if we use modified quicksort?