Deterministic and Randomized Quicksort Andreas Klappenecker.

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

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.
Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
CSE 3101: Introduction to the Design and Analysis of Algorithms
Quicksort Lecture 3 Prof. Dr. Aydın Öztürk. Quicksort.
Order Statistics(Selection Problem) A more interesting problem is selection:  finding the i th smallest element of a set We will show: –A practical randomized.
Introduction to Algorithms Jiafen Liu Sept
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Median/Order Statistics Algorithms
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 Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
Introduction to Algorithms Chapter 7: Quick Sort.
CS38 Introduction to Algorithms Lecture 7 April 22, 2014.
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.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Ch. 7 - QuickSort Quick but not Guaranteed. Ch.7 - QuickSort Another Divide-and-Conquer sorting algorithm… As it turns out, MERGESORT and HEAPSORT, although.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
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.
Median, order statistics. Problem Find the i-th smallest of n elements.  i=1: minimum  i=n: maximum  i= or i= : median Sol: sort and index the i-th.
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.
10 Algorithms in 20th Century Science, Vol. 287, No. 5454, p. 799, February 2000 Computing in Science & Engineering, January/February : The Metropolis.
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.
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.
September 29, Algorithms and Data Structures Lecture V Simonas Šaltenis Aalborg University
Introduction to Algorithms Jiafen Liu Sept
David Luebke 1 6/3/2016 CS 332: Algorithms Analyzing Quicksort: Average Case.
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.
Order Statistics(Selection Problem)
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSS106 Introduction to Elementary Algorithms M.Sc Askar Satabaldiyev Lecture 05: MergeSort & QuickSort.
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.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
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:
Mudasser Naseer 1 3/4/2016 CSC 201: Design and Analysis of Algorithms Lecture # 6 Bubblesort Quicksort.
Sorting. 2 The Sorting Problem Input: A sequence of n numbers a 1, a 2,..., a n Output: A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input.
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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677
Order Statistics.
Quick Sort Divide: Partition the array into two sub-arrays
Order Statistics Comp 122, Spring 2004.
Quick Sort (11.2) CSE 2011 Winter November 2018.
Ch 7: Quicksort Ming-Te Chi
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Order Statistics Comp 550, Spring 2015.
CS 583 Analysis of Algorithms
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
Quicksort Quick sort Correctness of partition - loop invariant
Presentation transcript:

Deterministic and Randomized Quicksort Andreas Klappenecker

Overview Deterministic Quicksort Modify Quicksort to obtain better asymptotic bound Linear-time median algorithm Randomized Quicksort

Deterministic Quicksort Quicksort(A,p,r) if p < r then q := Partition(A,p,r); // rearrange A[p..r] in place Quicksort(A, p,q-1); Quicksort(A,p+1,r);

Divide-and-Conquer The design of Quicksort is based on the divide-and-conquer paradigm. a) Divide : Partition the array A[p..r] into two (possibly empty) subarrays A[p..q-1] and A[q+1,r] such that - A[x] <= A[q] for all x in [p..q-1] - A[x] > A[q] for all x in [q+1,r] b) Conquer : Recursively sort A[p..q-1] and A[q+1,r] c) Combine : nothing to do here

Partition Select pivot (orange element) and rearrange: larger elements to the left of the pivot (red) elements not exceeding the pivot to the right (yellow) pir

Partition Partition(A,p,r) x := A[r]; // select rightmost element as pivot i := p-1; for j = p to r-1 do if A[j] <= x then i := i+1; swap(A[i], A[j]); fi; od; swap(A[i+1],A[r]) return i+1; Throughout the for loop: If p <= k <= i then A[k]<= x If i+1 x If k=r, then A[k] = x A[j..r-1] is unstructured

Partition - Loop - Example i p,jr p,ijr jr jr pijr pijr pijr pir

After the loop, the partition routine swaps the leftmost element of the right partition with the pivot element: swap(A[i+1],A[r]) now recursively sort yellow and red parts pir pir

Worst-Case Partitioning The worst-case behavior for quicksort occurs on an input of length n when partitioning produces just one subproblem with n-1 elements and one subproblem with 0 elements. Therefore the recurrence for the running time T(n) is: T(n) = T(n-1) + T(0) + θ(n) = T(n-1) + θ(n) = θ(n 2 ) Perhaps we should call this algorithm pokysort?

“Better” Quicksort and Linear Median Algorithm

Best-case Partitioning Best-case partitioning: If partition produces two subproblems that are roughly of the same size, then the recurrence of the running time is T(n) <= 2T(n/2) + θ(n) so that T(n) = O(n log n) Can we achieve this bound? Yes, modify the algorithm. Use a linear-time median algorithm to find median, then partition using median as pivot.

Linear Median Algorithm Let A[1..n] be an array over a totally ordered domain. - Partition A into groups of 5 and find the median of each group. [You can do that with 6 comparisons] - Make an array U[1..n/5] of the medians and find the median m of U by recursively calling the algorithm. - Partition the array A using the median-of-medians m to find the rank of m in A. If m is of larger rank than the median of A, eliminate all elements > m. If m is of smaller rank than the median of A, then eliminate all elements <= m. Repeat the search on the smaller array.

Linear-Time Median Finding How many elements do we eliminate in each round? The array U contains n/5 elements. Thus, n/10 elements of U are larger (smaller) than m, since m is the median of U. Since each element in U is a median itself, there are 3n/10 elements in A that are larger (smaller) than m. Therefore, we eliminate (3/10)n elements in each round. Thus, the time T(n) to find the median is T(n) <= T(n/5) + T(7n/10) + 6n/5. // median of U, recursive call, and finding medians of groups

Solving the Recurrence Suppose that T(n) <= cn (for some c to be determined later) T(n) <= c(n/5) + c(7n/10)+6n/5= c(9n/10)+6n/5 If this is to be <= cn, then we need to have c(9n/10)+12n/10 <= cn or 12 <= c Suppose that T(1) = d. Then choose c = max{12,d}. An easy proof by induction yields T(n) <= cn.

Goal Achieved? We can accomplish that quicksort achieves O(n log n) running time, if we use the linear-time median finding algorithm to select the pivot element. Unfortunately, the constant in the big Oh expression becomes large, and quicksort looses some of its appeal. Is there a simpler solution?

Randomized Quicksort

Deterministic Quicksort Randomized-Quicksort(A,p,r) if p < r then q := Randomized-Partition(A,p,r); Randomized-Quicksort(A, p,q-1); Randomized-Quicksort(A,p+1,r);

Partition Randomized-Partition(A,p,r) i := Random(p,r); swap(A[i],A[r]); Partition(A,p,r); Almost the same as Partition, but now the pivot element is not the rightmost element, but rather an element from A[p..r] that is chosen uniformly at random.

Goal The running time of quicksort depends mostly on the number of comparisons performed in all calls to the Randomized-Partition routine. Let X denote the random variable counting the number of comparisons in all calls to Randomized-Partition.

Notations Let z i denote the i-th smallest element of A[1..n]. Thus A[1..n] sorted is. Let Z ij = {z i,..., z j } denote the set of elements between z i and z j, including these elements. X ij = I{ z i is compared to z j }. Thus, X ij is an indicator random variable for the event that the i- th smallest and the j-th smallest elements of A are compared in an execution of quicksort.

Number of Comparisons Since each pair of elements is compared at most once by quicksort, the number X of comparisons is given Therefore, the expected number of comparisons is ddd

When do we compare? When do we compare z i to z j ? Suppose we pick a pivot element in Z ij = {z i,..., z j }. If z i < x < z j then z i and z j will land in different partitions and will never be compared afterwards. Therefore, z i and z j will be compared if and only if the first element of Z ij to be picked as pivot element is contained in the set {z i,z j }.

Probability of Comparison

Expected Number of Comparisons

Conclusion It follows that the expected running time of Randomized- Quicksort is O(n log n). It is unlikely that this algorithm will choose a terribly unbalanced partition each time, so the performance is very good almost all the time.