Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Introduction to Algorithms Jiafen Liu Sept
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Using Divide and Conquer for Sorting
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
1 Introduction to Randomized Algorithms Md. Aashikur Rahman Azim.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
7.Quicksort Hsu, Lih-Hsing. Computer Theory Lab. Chapter 7P Description of quicksort Divide Conquer Combine.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Quicksort!. A practical sorting algorithm Quicksort  Very efficient Tight code Good cache performance Sort in place  Easy to implement  Used in older.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
Efficient Algorithms Quicksort. Quicksort A common algorithm for sorting non-sorted arrays. Worst-case running time is O(n 2 ), which is actually the.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Chapter 7 Quicksort Ack: This presentation is based on the lecture slides from Hsu, Lih- Hsing, as well as various materials from the web.
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.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
Chapter 9: Selection Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
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.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Deterministic and Randomized Quicksort Andreas Klappenecker.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Young CS 331 D&A of Algo. Topic: Divide and Conquer1 Divide-and-Conquer General idea: Divide a problem into subprograms of the same kind; solve subprograms.
Divide and Conquer (Part II) Multiplication of two numbers Let U = (u 2n-1 u 2n-2 … u 1 u 0 ) 2 and V = (v 2n-1 v 2n-2 …v 1 v 0 ) 2, and our goal is to.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
David Luebke 1 2/19/2016 Priority Queues Quicksort.
CSC317 1 Quicksort on average run time We’ll prove that average run time with random pivots for any input array is O(n log n) Randomness is in choosing.
QuickSort. Yet another sorting algorithm! Usually faster than other algorithms on average, although worst-case is O(n 2 ) Divide-and-conquer: –Divide:
Computer Sciences Department1. Sorting algorithm 4 Computer Sciences Department3.
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
Chapter 9: Selection of Order Statistics What are an order statistic? min, max median, i th smallest, etc. Selection means finding a particular order statistic.
Analysis of Algorithms CS 477/677
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Ch 7: Quicksort Ming-Te Chi
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Ack: Several slides from Prof. Jim Anderson’s COMP 202 notes.
Chapter 7 Quicksort.
Order Statistics Comp 122, Spring 2004.
The Selection Problem.
Quicksort and Randomized Algs
Design and Analysis of Algorithms
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

Ch. 7 - QuickSort Quick but not Guaranteed

Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although O(n lg n) in their time complexity, have fairly large constants and tend to move data around more than desirable (e.g., equal-key items may not maintain their relative position from input to output). We introduce another algorithm with better constants, but a flaw: its worst case in O(n 2 ). Fortunately, the worst case is “rare enough” so that the speed advantages work an overwhelming amount of the time… and it is O(n lg n) on average. 6/13/

Ch.7 - QuickSort Like in MERGESORT, we use Divide-and-Conquer: 1.Divide: partition A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is ≤ A[q], and each element of A[q+1..r] is ≥ A[q]. Compute q as part of this partitioning. 2.Conquer: sort the subarrays A[p..q-1] and A[q+1..r] by recursive calls to QUICKSORT. 3.Combine: the partitioning and recursive sorting leave us with a sorted A[p..r] – no work needed here. An obvious difference is that we do most of the work in the divide stage, with no work at the combine one. 6/13/

Ch.7 - QuickSort The Pseudo-Code 6/13/

Ch.7 - QuickSort 6/13/

Ch.7 - QuickSort Proof of Correctness: PARTITION We look for a loop invariant and we observe that at the beginning of each iteration of the loop (l.3-6) for any array index k : 1.If p ≤ k ≤ i, then A[k] ≤ x ; 2.If i+1 ≤ k ≤ j-1, then A[k] > x ; 3.If k = r, then A[k] = x. 4.If j ≤ k ≤ r-1, then we don’t know anything about A[k]. 6/13/

Ch.7 - QuickSort The Invariant Initialization. Before the first iteration: i=p-1, j=p. No values between p and i ; no values between i+1 and j-1. The first two conditions are trivially satisfied; the initial assignment satisfies 3. Maintenance. Two cases –1. A[j] > x. –2. A[j] ≥ x. 6/13/

Ch.7 - QuickSort The Invariant Termination. j=r. Every entry in the array is in one of the three sets described by the invariant. We have partitioned the values in the array into three sets: less than or equal to x, greater than x, and a singleton containing x. Running time of PARTITION on A[p..r] is  (n), where n = r – p /13/

Ch.7 - QuickSort QUICKSORT : Performance – a quick look. We first look at (apparent) worst-case partitioning: T(n) = T(n-1) + T(0) +  (n) = T(n-1) +  (n). It is easy to show – using substitution - that T(n) =  (n 2 ). We next look at (apparent) best-case partitioning: T(n) = 2T(n/2) +  (n). It is also easy to show (case 2 of the Master Theorem) that T(n) =  (n lg n). Since the disparity between the two is substantial, we need to look further… 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Balanced Partitioning 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – the Average Case As long as the number of “good splits” is bounded below as a fixed percentage of all the splits, we maintain logarithmic depth and so O(n lg n) time complexity. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Randomized QUICKSORT We would like to ensure that the choice of pivot does not critically impair the performance of the sorting algorithm – the discussion to this point would indicate that randomizing the choice of the pivot should provide us with good behavior (if at all possible with the data-set we are trying to sort). We introduce 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Randomized QUICKSORT And the recursive procedure becomes: Every call to RANDOMIZED-PARTITION has introduced the (constant) extra overhead of a call to RANDOM. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Rigorous Worst Case Analysis Since we do not, a priori, have any idea of what the splits of the subarrays will be, we have to represent a possible “worst case” (we already have an O(n 2 ) bound from the “bad split” example – so it could be worse… although we hope not). The worst case leads to the recurrence T(n) = max 0≤q≤n-1 (T(q) + T(n – q - 1)) +  (n), where we remember that the pivot does not appear at the next level (down) of the recursion. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Rigorous Worst Case Analysis We have to come up with a “guess” and the basis for the guess is our likely “bad split case”: it tells us we cannot hope for any better than  (n 2 ). So we just hope it is no worse… Guess T(n) ≤ cn 2 for some c > 0 and start doing algebra for the induction: T(n) ≤ max 0≤q≤n-1 (T(q) + T(n – q - 1)) +  (n) ≤ max 0≤q≤n-1 (cq 2 + c(n – q - 1) 2 ) +  (n). Differentiate cq 2 + c(n – q - 1) 2 twice with respect to q, to obtain 4c > 0 for all values of q. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Rigorous Worst Case Analysis Since the expression represents a quadratic curve, concave up, it reaches it maximum at one of the endpoints q = 0 and q = n – 1. As we evaluate, we find max 0≤q≤n-1 (cq 2 + c(n – q - 1) 2 ) +  (n)≤ c max 0≤q≤n-1 (q 2 + (n – q - 1) 2 ) +  (n)≤ c (n – 1) 2 +  (n) = cn 2 – 2cn  (n) ≤ cn 2 by choosing c large enough to overcome the positive constant in  (n). 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime Understanding partitioning. 1.Each time PARTITION is called, it selects a pivot element and this pivot element is never included in successive calls: the total number of calls to PARTITION is n. 2.Each call to PARTITION costs O(1) plus an amount of time proportional to the number of iterations of the for loop. 3.Each iteration of the for loop (in line 4) performs a comparison, comparing the pivot to another element in A. 4.We need to count the number of times l. 4 is executed. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime Lemma 7.1. Let X be the number of comparisons performed in l. 4 of PARTITION over the entire execution of QUICKSORT on an n -element array. Then the running time of QUICKSORT is O(n + X). Proof: the observations on the previous slide. We need to find X, the total number of comparisons performed over all calls to PARTITION. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime 1.Rename the elements of A as z 1, z 2, …, z n, so that z i is the i th smallest element of A. 2.Define the set Z ij = {z i, z i+1,…, z j }. 3.Question: when does the algorithm compare z i and z j ? 4.Answer: at most once – notice that all elements in every (sub)array are compared to the pivot once, and will never be compared to the pivot again (since the pivot is removed from the recursion). 5.Define X ij = I{z i is compared to z j }, the indicator variable of this event. Comparisons are over the full run of the algorithm. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime 6.Since each pair is compared at most once, we can write 7.Taking expectations of both sides: 8.We need to compute Pr{z i is compared to z j }. 9.We will assume all z i and z j are distinct. 10.For any pair z i, z j, once a pivot x is chosen so that z i < x < z j, z i and z j will never be compared again (why?). 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime 11.If z i is chosen as a pivot before any other item in Z ij, then z i will be compared to every other item in Z ij. 12.Same for z j. 13. z i and z j are compared if and only if the first element to be chosen as a pivot from Z ij is either z i or z j. 14.What is that probability? Until a point of Z ij is chosen as a pivot, the whole of Z ij is in the same partition, so every element of Z ij is equally likely to be the first one chosen as a pivot. 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime 15.Because Z ij has j – i + 1 elements, and because pivots are chosen randomly and independently, the probability that any given element is the first one chosen as a pivot is 1/(j-i+1). It follows that: 16. Pr{z i is compared to z j } = Pr{z i or z j is first pivot chosen from Z ij } = Pr{z i is first pivot chosen from Z ij }+ Pr{ z j is first pivot chosen from Z ij } = 1/(j-i+1) + 1/(j-i+1) = 2/(j-i+1). 6/13/

Ch.7 - QuickSort QUICKSORT : Performance – Expected RunTime 17.Replacing the right-hand-side in 7, and grinding through some algebra: And the result follows. 6/13/