slides adapted from Marty Stepp

Slides:



Advertisements
Similar presentations
ADA: 5. Quicksort1 Objective o describe the quicksort algorithm, it's partition function, and analyse its running time under different data conditions.
Advertisements

Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
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
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:
QuickSort The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
CS 162 Intro to Programming II Quick Sort 1. Quicksort Maybe the most commonly used algorithm Quicksort is also a divide and conquer algorithm Advantage.
CSE 373: Data Structures and Algorithms
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. Sorting III 1 An Introduction to Sorting.
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
Quicksort.
Quicksort.
Unit 061 Quick Sort csc326 Information Structures Spring 2009.
CS2420: Lecture 11 Vladimir Kulyukin Computer Science Department Utah State University.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
CSE 143 Lecture 16 Sorting reading: 13.1, slides created by Marty Stepp
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
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.
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Advanced Sorting.
Prof. U V THETE Dept. of Computer Science YMA
COP 3503 FALL 2012 Shayan Javed Lecture 16
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.
Data Structures and Algorithms
CSE 143 Lecture 23: quick sort.
Quicksort 1.
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Quick Sort (11.2) CSE 2011 Winter November 2018.
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
CO 303 Algorithm Analysis And Design Quicksort
CSC215 Lecture Algorithms.
CSE 154 Sorting reading: 13.3, 13.4
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Marty Stepp
Sub-Quadratic Sorting Algorithms
slides created by Marty Stepp
EE 312 Software Design and Implementation I
slides created by Marty Stepp
CSE 143 Sorting reading: 13.3, 13.4.
Quicksort.
CSE 373: Data Structures and Algorithms
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
slides created by Marty Stepp and Hélène Martin
Algorithms: Design and Analysis
slides adapted from Marty Stepp
Data Structures & Algorithms
CSE 373 Sorting 1: Bogo Sort, Stooge Sort, Bubble Sort
Quicksort.
Insertion Sort and Shell Sort
CSE 373 Sorting 2: Selection, Insertion, Shell Sort
CSE 332: Sorting II Spring 2016.
Quicksort.
Stacks, Queues, ListNodes
Sorting Popular algorithms:
Presentation transcript:

slides adapted from Marty Stepp CSE 143 Lecture 26 quick sort slides adapted from Marty Stepp http://www.cs.washington.edu/143/

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.

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

Partitioning an array Swap the pivot to the last array slot, temporarily. Repeat until done partitioning (until i,j meet): Starting from i = 0, find an element a[i] ≥ pivot. Starting from j = N-1, find an element a[j] ≤ pivot. These elements are out of order, so swap a[i] and a[j]. Swap the pivot back to index i to place it between the partitions. index 1 2 3 4 5 6 7 8 9 value 8 i  j 6 2 i  8 5 9 1 4 3 7

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 ... ...

Quick sort code public static void quickSort(int[] a) { quickSort(a, 0, a.length - 1); } private static void quickSort(int[] a, int min, int max) { if (min >= max) { // base case; no need to sort return; // choose pivot; we'll use the first element (might be bad!) int pivot = a[min]; swap(a, min, max); // move pivot to end // partition the two sides of the array int middle = partition(a, min, max - 1, pivot); swap(a, middle, max); // restore pivot to proper location // recursively sort the left and right partitions quickSort(a, min, middle - 1); quickSort(a, middle + 1, max);

Partition code // partitions a with elements < pivot on left and // elements > pivot on right; // returns index of element that should be swapped with pivot private static int partition(int[] a, int i, int j, int pivot) { while (i <= j) { // move index markers i,j toward center // until we find a pair of out-of-order elements while (i <= j && a[i] < pivot) { i++; } while (i <= j && a[j] > pivot) { j--; } if (i <= j) { swap(a, i, j); i++; j--; } return i;

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, as is shell sort. 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.