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.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
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
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
© 2004 Goodrich, Tamassia QuickSort1 Quick-Sort     29  9.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Quick-Sort     29  9.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
Topic 17 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.
CS 206 Introduction to Computer Science II 12 / 05 / 2008 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 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
Quicksort
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
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.
Computer Science Searching & Sorting.
1 Sorting and Searching "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.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
© 2004 Goodrich, Tamassia Quick-Sort     29  9.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Merge Sort 7 2  9 4   2  2 79  4   72  29  94  4.
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.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
QuickSort by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data.
CS 367 Introduction to Data Structures Lecture 11.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Prof. U V THETE Dept. of Computer Science YMA
Chapter 11 Sorting Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount.
Fundamental Data Structures and Algorithms
Quicksort
Searching & Sorting "There's nothing hidden 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.
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,
Quick-Sort 9/13/2018 1:15 AM Quick-Sort     2
Chapter 7 Sorting Spring 14
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."
CSE 143 Lecture 23: quick sort.
Description Given a linear collection of items x1, x2, x3,….,xn
Quicksort 1.
Topic 14 Searching and Simple Sorts
Sorting and Searching "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,
Quick Sort (11.2) CSE 2011 Winter November 2018.
CO 303 Algorithm Analysis And Design Quicksort
Unit-2 Divide and Conquer
8/04/2009 Many thanks to David Sun for some of the included slides!
Sub-Quadratic Sorting Algorithms
slides adapted from Marty Stepp
Topic 14 Searching and Simple Sorts
Topic 17 Faster 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.
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.
Topic 24 sorting and searching arrays
EE 312 Software Design and Implementation I
Quick-Sort 2/25/2019 2:22 AM Quick-Sort     2
Quicksort.
Quick-Sort 4/8/ :20 AM Quick-Sort     2 9  9
CS 1114: Sorting and selection (part two)
CSE 373 Data Structures and Algorithms
CSC 143 Java Sorting.
Quick-Sort 4/25/2019 8:10 AM Quick-Sort     2
Data Structures & Algorithms
Quicksort.
CS200: Algorithm Analysis
Design and Analysis of Algorithms
Searching and Sorting Hint at Asymptotic Complexity
Quicksort.
Data Structures and Algorithms CS 244
Presentation transcript:

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 and the Sorcerer's Stone

Quicksort Quicksort is the most popular fast sorting algorithm: It has an average running time case of Θ(n*log n) The other fast sorting algorithms, Mergesort and Heapsort, also run in Θ(n*log n) time, but have fairly large constants hidden in their algorithms tend to move data around more than desirable CS 321 - Data Structures

Quicksort Invented by C.A.R. (Tony) Hoare A Divide-and-Conquer approach that uses recursion: If the list has 0 or 1 elements, it’s sorted Otherwise, pick any element p in the list. This is called the pivot value Partition the list, minus the pivot, into two sub-lists: one of values less than the pivot and another of those greater than the pivot equal values go to either Return the Quicksort of the first list followed by the Quicksort of the second list. CS 321 - Data Structures

Quicksort Tree Use binary tree to represent the execution of Quicksort Each node represents a recursive call of Quicksort Stores Unsorted sequence before the execution and its pivot Sorted sequence at the end of the execution The leaves are calls on sub-sequences of size 0 or 1 9 7 2 4 6  2 4 6 7 9 2 4  2 4 9 7  7 9 2  2 - - 9  9 CS 321 - Data Structures

Example: Quicksort Execution Pivot selection – last value in list: 6 7 9 4 3 7 1 2 6 - CS 321 - Data Structures

Example: Quicksort Execution Partition around 6 Recursive call on sub-list with smaller values 7 9 4 3 7 1 2 6 - 4 3 1 2 - 7 9 7 - CS 321 - Data Structures

Example: Quicksort Execution Partition around 2 Recursive call on sub-list of smaller values 7 9 4 3 7 1 2 6 - 4 3 1 2 - 7 9 7 - 1 - 4 3 - CS 321 - Data Structures

Example: Quicksort Execution Base case Return from sub-list of smaller values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 7 9 7 - 1  1 4 3 - CS 321 - Data Structures

Example: Quicksort Execution Recursive call on sub-list of larger values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 7 9 7 - 1  1 4 3 - CS 321 - Data Structures

Example: Quicksort Execution Partition around 4 Recursive call on sub-list of smaller values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 7 9 7 - 1  1 4 3 - 3 - - CS 321 - Data Structures

Example: Quicksort Execution Base case Return from sub-list of smaller values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 7 9 7 - 1  1 4 3  3 4 3  3 - CS 321 - Data Structures

Example: Quicksort Execution Recursive call on sub-list of larger values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 7 9 7 - 1  1 4 3  3 4 3  3 - CS 321 - Data Structures

Example: Quicksort Execution Base case Return from sub-list of larger values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 7 9 7 - 1  1 4 3  3 4 3  3 - CS 321 - Data Structures

Example: Quicksort Execution Return from sub-list of larger values 7 9 4 3 7 1 2 6 - 4 3 1 2  1 2 3 4 7 9 7 - 1  1 4 3  3 4 3  3 - CS 321 - Data Structures

Example: Quicksort Execution Return from sub-list of smaller values 7 9 4 3 7 1 2 6  1 2 3 4 6 4 3 1 2  1 2 3 4 7 9 7 - 1  1 4 3  3 4 3  3 - CS 321 - Data Structures

Example: Quicksort Execution Recursive call on sub-list of larger values 7 9 4 3 7 1 2 6  1 2 3 4 6 4 3 1 2  1 2 3 4 7 9 7 - 1  1 4 3  3 4 3  3 - CS 321 - Data Structures

Example: Quicksort Execution Partition around 7 Recursive call on sub-list of smaller values 7 9 4 3 7 1 2 6  1 2 3 4 6 4 3 1 2  1 2 3 4 7 9 7 - 1  1 4 3  3 4 7 - 9 - 3  3 3 CS 321 - Data Structures

Example: Quicksort Execution Base case Return from sub-list of smaller values 7 9 4 3 7 1 2 6  1 2 3 4 6 4 3 1 2  1 2 3 4 7 9 7  7 7 1  1 4 3  3 4 7  7 9 - 3  3 CS 321 - Data Structures

Example: Quicksort Execution Recursive call on sub-list of larger values 7 9 4 3 7 1 2 6  1 2 3 4 6 4 3 1 2  1 2 3 4 7 9 7  7 7 1  1 4 3  3 4 7  7 9 - 3  3 CS 321 - Data Structures

Example: Quicksort Execution Base case Return from sub-list of larger values 7 9 4 3 7 1 2 6  1 2 3 4 6 4 3 1 2  1 2 3 4 7 9 7  7 7 9 1  1 4 3  3 4 7  7 9  9 3  3 CS 321 - Data Structures

Example: Quicksort Execution Return from sub-list of larger values 7 9 4 3 7 1 2 6  1 2 3 4 6 7 7 9 4 3 1 2  1 2 3 4 7 9 7  7 7 9 1  1 4 3  3 4 7  7 9  9 3  3 CS 321 - Data Structures

Example: Quicksort Execution Done 7 9 4 3 7 1 2 6  1 2 3 4 6 7 7 9 4 3 1 2  1 2 3 4 7 9 7  7 7 9 1  1 4 3  3 4 7  7 9  9 3  3 CS 321 - Data Structures

Quicksort Algorithm // A: array of values // p: beginning index of sub-list being sorted // r: ending index of sub-list being sorted // Initial call: Quicksort(A, 1, A.length) CS 321 - Data Structures

Quicksort Partition Algorithm CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) x = A[7] i = p - 1 (0) j = 1 A[1] ≤ 6 i = 0 + 1 swap A[1], A[1] 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) j = 2 A[2] > 6 no change 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) j = 3 A[3] > 6 no change 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) j = 4 A[4] ≤ 6 i = 1 + 1 swap A[2], A[4] 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) j = 5 A[5] ≤ 6 i = 2 + 1 swap A[3], A[5] 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) j = 6 A[6] ≤ 6 i = 3 + 1 swap A[4], A[6] 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) j = 7 for loop done swap A[5], A[7] return 5 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Example: Quicksort Partition Partition(A, 1, A.length) Partition complete 1 2 3 4 5 6 7 8 CS 321 - Data Structures

Runtime Analysis: Best Case What is best case running time? Assume keys are random, uniformly distributed. Recursion: Partition splits list in two sub-lists of size (n/2) Quicksort each sub-list Depth of recursion tree? O(log n) Number of accesses in partition? O(n) Best case running time: O(n*log n) CS 321 - Data Structures

Runtime Analysis: Worst Case What is worst case running time? List already sorted Recursion: Partition splits array in two sub-arrays: one sub-array of size 0 the other sub-array of size n-1 Quicksort each sub-array Depth of recursion tree? O(n) Number of accesses per partition? O(n) Worst case running time: O(n2) CS 321 - Data Structures

Average Case for Quicksort If the list is already sorted, Quicksort is terrible: O(n2) It is possible to construct other bad cases However, Quicksort is usually O(n*log n) The constants are so good, Quicksort is generally the fastest known algorithm Most real-world sorting is done by Quicksort CS 321 - Data Structures

Tweaking Quicksort Almost anything you can try to “improve” Quicksort will actually slow it down One good tweak is to switch to a different sorting method when the subarrays get small (say, 10 or 12) Quicksort has too much overhead for small array sizes For large arrays, it might be a good idea to check beforehand if the array is already sorted But there is a better tweak than this CS 321 - Data Structures

Picking a Better Pivot Before, we picked the first element of the sub-list to use as a pivot If the array is already sorted, this results in O(n2) behavior It’s no better if we pick the last element We could do an optimal quicksort if we always picked a pivot value that exactly cuts the array in half Such a value is called a median: half of the values in the list are larger, half are smaller The easiest way to find the median is to sort the list and pick the value in the middle (!) CS 321 - Data Structures

Median of Three Obviously, it doesn’t make sense to sort the list in order to find the median Instead, compare just three elements of sub-list: first, last, and middle Take the median (middle value) of these three as pivot If rearrange (sort) these three numbers so that the smallest is in the first position, the largest in the last position, and the other in the middle Simplifies and speeds up the partition loop CS 321 - Data Structures

Summary of Sorting Algorithms Time Notes Selection Sort O(n2) in-place slow (good for small inputs) Insertion Sort Quicksort O(n*log n) (expected) in-place, randomized fastest (good for large inputs) Heapsort O(n*log n) fast (good for large inputs) Mergesort sequential data access fast (good for huge inputs) CS 321 - Data Structures

CS 321 - Data Structures

Quicksort Algorithm public void quicksort(Comparable list[], int lo, int hi) { if(lo < hi) int p = partition(list, lo, hi); quicksort(list, lo, p - 1); quicksort(list, p + 1, hi); } public static void swap(Object a[], int index1, int index2) Object tmp = a[index1]; a[index1] = a[index2]; a[index2] = tmp; CS 321 - Data Structures

Quicksort Partition Algorithm public int partition(Comparable list[], int lo, int hi) { // pivot at start position int pivot = list[lo]; // keep track of last value <= pivot int i = lo; // move smaller values down in list for(int j = lo + 1; j <= hi) if(list[j] <= pivot) i++; swap(list[i], list[j]); } // put pivot in correct position in partitioned list swap(list[i], list[lo]); // index of pivot value return i; CS 321 - Data Structures

Comparison of Various Sorts Num Items Selection Insertion Quicksort 1000 16 5 2000 59 49 6 4000 271 175 8000 1056 686 16000 4203 2754 11 32000 16852 11039 45 64000 expected? 68 128000 158 256000 335 512000 722 1024000 1550 times in milliseconds CS 321 - Data Structures