CSE 143 Lecture 23: quick sort.

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
Quicksort Quicksort     29  9.
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.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CSE 373: Data Structures and Algorithms
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
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.
Quicksort, Mergesort, and Heapsort. Quicksort Fastest known sorting algorithm in practice  Caveats: not stable  Vulnerable to certain attacks Average.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 206 Introduction to Computer Science II 12 / 09 / 2009 Instructor: Michael Eckmann.
General Computer Science for Engineers CISC 106 James Atlas Computer and Information Sciences 10/23/2009.
Sorting Chapter 10.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Quicksort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
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.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Advanced Sorting.
School of Computing Clemson University Fall, 2012
Sorting.
Algorithm Efficiency and Sorting
Divide and Conquer Strategy
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.
Week 12 - Wednesday CS221.
Fast Sorting "The bubble sort seems to have nothing to recommend it, except a catchy name and the fact that it leads to some interesting theoretical problems."
Chapter 4: Divide and Conquer
Data Structures and Algorithms
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Quicksort analysis Bubble sort
8/04/2009 Many thanks to David Sun for some of the included slides!
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Chapter 4.
CSE 326: Data Structures Sorting
EE 312 Software Design and Implementation I
Quicksort.
CSE 373: Data Structures and Algorithms
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
Sorting Chapter 10.
Data Structures & Algorithms
Divide & Conquer Sorting
Algorithm Efficiency and Sorting
Design and Analysis of Algorithms
CSE 332: Sorting II Spring 2016.
Lecture 19: Sorting Algorithms
Algorithm Efficiency and Sorting
Quicksort.
Data Structures and Algorithms CS 244
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

CSE 143 Lecture 23: quick sort

Quick sort quick sort: Orders a list of values by partitioning the list around one element called a pivot, then sorting each partition. invented by British computer scientist C.A.R. Hoare in 1960 Quick sort is another divide and conquer algorithm: Choose one element in the list to be the pivot. Divide the elements so that all elements less than the pivot are to its left and all greater (or equal) are to its right. Conquer by applying quick sort (recursively) to both partitions. Runtime: O(N log N) average, O(N2) worst case. Generally somewhat faster than merge sort.

Quick sort example index 1 2 3 4 5 6 7 8 9 value 65 23 81 43 92 39 57 1 2 3 4 5 6 7 8 9 value 65 23 81 43 92 39 57 16 75 32 choose pivot=65 32 23 81 43 92 39 57 16 75 65 swap pivot (65) to end swap 81, 16 swap 57, 92 swap pivot back in recursively quicksort each half 32 23 16 43 57 39 pivot=32 swap to end swap 39, 16 swap 32 back in 81 75 92 pivot=81 swap to end swap 92, 75 swap 81 back in ... ...

Choosing a "pivot" The algorithm will work correctly no matter which element you choose as the pivot. A simple implementation can just use the first element. But for efficiency, it is better if the pivot divides up the array into roughly equal partitions. What kind of value would be a good pivot? A bad one? index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 18 -4 27 30 36 50 68 91 56 85 42 98 25

Choosing a better pivot Choosing the first element as the pivot leads to very poor performance on certain inputs (ascending, descending) does not partition the array into roughly-equal size chunks Alternative methods of picking a pivot: random: Pick a random index from [min .. max] median-of-3: look at left/middle/right elements and pick the one with the medium value of the three: a[min], a[(max+min)/2], and a[max] better performance than picking random numbers every time provides near-optimal runtime for almost all input orderings index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value 18 91 -4 27 30 86 50 65 78 56 25 42 98 31

Stable sorting stable sort: One that maintains relative order of "equal" elements. important for secondary sorting, e.g. sort by name, then sort again by age, then by salary, ... All of the N2 sorts shown are stable. bubble, selection, insertion, shell Merge sort is stable. Quick sort is not stable. The partitioning algorithm can reverse the order of "equal" elements. For this reason, Java's Arrays/Collections.sort() use merge sort.

Unstable sort example Suppose you want to sort these points by Y first, then by X: [(4, 2), (5, 7), (3, 7), (3, 1)] A stable sort like merge sort would do it this way: [(3, 1), (4, 2), (5, 7), (3, 7)] sort by y [(3, 1), (3, 7), (4, 2), (5, 7)] sort by x Note that the relative order of (3, 1) and (3, 7) is maintained. Quick sort might leave them in the following state: [(3, 7), (3, 1), (4, 2), (5, 7)] sort by x Note that the relative order of (3, 1) and (3, 7) has reversed.