1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.

Slides:



Advertisements
Similar presentations
David Luebke 1 4/22/2015 CS 332: Algorithms Quicksort.
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
Introduction to Algorithms
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.
Using Divide and Conquer for Sorting
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different approaches –Probabilistic analysis of a deterministic algorithm –Randomized.
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.
Chapter 7: Sorting Algorithms
Introduction to Algorithms Chapter 7: Quick Sort.
CSC 2300 Data Structures & Algorithms March 23, 2007 Chapter 7. Sorting.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
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.
September 19, Algorithms and Data Structures Lecture IV Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
CSC 2300 Data Structures & Algorithms March 27, 2007 Chapter 7. Sorting.
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.
CSC 2300 Data Structures & Algorithms March 20, 2007 Chapter 7. Sorting.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Design and Analysis of Algorithms – Chapter 51 Divide and Conquer (I) Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Mergesort and Quicksort Chapter 8 Kruse and Ryba.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
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.
MS 101: Algorithms Instructor Neelima Gupta
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.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
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
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.
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.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
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.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
Analysis of Algorithms CS 477/677
Quick Sort Divide: Partition the array into two sub-arrays
Ch 7: Quicksort Ming-Te Chi
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
CS 583 Analysis of Algorithms
CS 332: Algorithms Quicksort David Luebke /9/2019.
Chapter 7 Quicksort.
Chapter 9: Medians and Order Statistics
The Selection Problem.
Quicksort and Randomized Algs
Design and Analysis of Algorithms
Presentation transcript:

1 Chapter 7 Quicksort

2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case

3 Cinderella’s Problem I see… You have to find the largest bolt and the largest nut

4 Cinderella’s New Problem I see… You have to sort the bolts and sort the nuts

5 Fairy Godmother’s Proposal 1.Pick one of the nut 2.Compare this nut with all other bolts  Find those which are larger, and find those which are smaller

6 Fairy Godmother’s Proposal Bolts smaller than nut Bolts larger than nut Bolt equal to nut Done ! picked nut

7 Fairy Godmother’s Proposal 3.Pick the bolt that is equal to the selected nut 4.Compare this bolt with all other nuts  Find those which are larger, and find those which are smaller Bolts smaller than nut Bolts larger than nut Bolt equal to nut

8 Fairy Godmother’s Proposal Nuts smaller than bolt Nuts larger than bolt Done !

9 Fairy Godmother’s Proposal 5.Sort left part (recursively) 6.Sort right part (recursively) ^^ This is all of my proposal ^^ Nuts smaller than bolt Nuts larger than bolt

10 Fairy Godmother’s Proposal Can you see why Fairy Godmother’s proposal is a correct algorithm? What is the running time ? Worst-case:  (n 2 ) comparisons No better than the brute force approach !! Though worst-case runs badly, the average case is good:  (n log n) comparisons

11 Quicksort uses Partition The previous algorithm is exactly Quicksort, which makes use of a Partition function: Partition(A,p,r) /* to partition array A[p..r] */ 1. Pick an element, say A[t] (called pivot) 2. Let q = #elements less than pivot 3. Put elements less than pivot to A[p..p+q-1] 4. Put pivot to A[p+q] 5. Put remaining elements to A[p+q+1..r] 6. Return q

12 More on Partition After Partition(A,p,r), we obtain the value q, and know that Pivot was A[p+q] Before A[p+q] : smaller than pivot After A[p+q] : larger than pivot There are many ways to perform Partition. One way is shown in the next slides It will be an in-place algorithm (using O(1) extra space in addition to the input array)

13 Ideas for In-Place Partition Idea 1: Use A[r] ( the last element ) as pivot Idea 2: Process A[p..r] from left to right The prefix ( the beginning part ) of A stores all elements less than pivot seen so far Use two counters: One for the length of the prefix One for the element we are looking

14 In-Place Partition in Action pivot next element Length of prefix = 0 before running Because next element is less than pivot, we shall extend the prefix by 1

15 In-Place Partition in Action pivot next element Length of prefix = 1 after 1 step Because next element is smaller than pivot, and is adjacent to the prefix, we extend the prefix

16 In-Place Partition in Action pivot next element Length of prefix = 2 after 2 steps Because next element is larger than pivot, no change to prefix

17 In-Place Partition in Action pivot next element Length of prefix = 2 after 3 steps Again, next element is larger than pivot, no change to prefix

18 In-Place Partition in Action pivot next element Length of prefix = 2 after 4 steps Because next element is less than pivot, we shall extend the prefix by swapping

19 In-Place Partition in Action pivot next element Length of prefix = 3 after 5 steps Because next element is larger than pivot, no change to prefix

20 In-Place Partition in Action pivot next element Length of prefix = 3 after 6 steps Because next element is less than pivot, we shall extend the prefix by swapping

21 In-Place Partition in Action pivot next element Length of prefix = 4 after 7 steps When next element is the pivot, we put it after the end of the prefix by swapping

22 In-Place Partition in Action pivot Length of prefix = 4 after 8 steps Partition is done, and return length of prefix

23 Quicksort The Quicksort algorithm works as follows: Quicksort(A,p,r) /* to sort array A[p..r] */ 1. if ( p  r ) return; 2. q = Partition(A,p,r); 3. Quicksort(A, p, p+q-1); 4. Quicksort(A, p+q+1, r); To sort A[1..n], we just call Quicksort(A,1,n)

24 Randomized Versions of Partition

25 Worst-Case Running Time The worst-case running time of Quicksort can be expressed by: T(n) = max q=0 to n-1 ( T(q) + T(n-q-1) ) +  (n) We prove T(n)=  (n 2 ) by substitution method: 1.Guess T(n) ≤ cn 2 for some constant c 2.Next, verify our guess by induction

26 Worst-Case Running Time Inductive Case: T(n) = max q=0 to n-1 ( T(q) + T(n-q-1) ) +  (n) ≤ max q=0 to n-1 ( cq 2 + c(n-q-1) 2 ) +  (n) ≤ c(n-1) 2 +  (n) = cn 2 - 2cn + c +  (n) ≤ cn 2 when c is large enough Maximized when q = 0 or when q = n-1 Inductive Case is OK now. How about Base Case?

27 Worst-Case Running Time Conclusion: 1. T(n) =  (n 2 ) 2. However, we can also show T(n) =  (n 2 ) by finding a worst-case input Let T(n) = T(n-1) +  (n)  T(n) =  (n 2 )

28 Balanced partitioning Quicksort’s average running time is much closer to the best case than to the worst case Imagine that PARTITION always produces a 9- to-1 split. Get the recurrence – T (n) = T(9n/10) + T(n/10) + c n Any split of constant proportionality will yield a recursion tree of depth Θ(lg n) It’s like the one for T(n) = T(n/3) + T(2n/3) + O(n) in Section 4.4.

29 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

30 Average-Case Running Time So, Quicksort runs badly for some input… But suppose that when we store a set of n numbers into the input array, each of the n! permutations are equally likely  Running time varies on input What will be the “average” running time ?

31 Average Running Time Let X = # comparisons in all Partition Later, we will show that Running time =  ( n + X ) varies on input Finding average of X (i.e. #comparisons) gives average running time Our first target: Compute average of X

32 Average # of Comparisons We define some notation to help the analysis: Let a 1, a 2, …, a n denote the set of n numbers initially placed in the array Further, we assume a 1  a 2  …  a n (So, a 1 may not be the element in A[1] originally) Let X ij = # comparisons between a i and a j in all Partition calls

33 Average # of Comparisons Then, X = # comparisons in all Partition calls = X 12 + X 13 + … + X n-1,n  Average # comparisons = E[X] = E[X 12 + X 13 + … + X n-1,n ] = E[X 12 ] + E[X 13 ] + … + E[X n-1,n ]

34 Average # of Comparisons The next slides will prove: E[X ij ] = 2/(j-i+1) Using this result, E[X] =  i=1 to n-1  j=i+1 to n 2/(j-i+1) =  i=1 to n-1  k=1 to n-i 2/(k+1)   i=1 to n-1  k=1 to n 2/k =  i=1 to n-1  (log n) =  (n log n)

35 Comparison between a i and a j Question: # times a i be compared with a j ? Answer: At most once, which happens only if a i or a j are chosen as pivot pivot After that, the pivot is fixed and is never compared with the others

36 Comparison between a i and a j Question: Will a i always be compared with a j ? Answer: No. E.g., after Partition in Page 14: pivot we will separately Quicksort the first 4 elements, and then the last 3 elements  3 is never compared with 8

37 Comparison between a i and a j Observation: Consider the elements a i, a i+1, …, a j-1, a j (i)If a i or a j is first chosen as a pivot, then a i is compared with a j (ii) Else, if any element of a i+1, …, a j-1 is first chosen as a pivot, then a i is never compared with a j

38 Comparison between a i and a j When the n! permutations are equally likely to be the input, Pr(a i compared with a j once) = 2/(j-i+1) Pr(a i not compared with a j ) = (j-i-1)/(j-i+1)  E[X ij ] = 1 * 2/(j-i+1) + 0 * (j-i-1)/(j-i+1) = 2/(j-i+1)

39 Observe that in the Quicksort algorithm: Each Partition fixes the position of pivot  at most n Partition calls After each Partition, we have 2 Quicksort Also, all Quicksort (except 1st one: Quicksort(A,1,n)) are invoked after a Partition  total  (n) Quicksort calls Proof: Running time =  (n+X)

40 So, if we ignore the comparison time in all Partition calls, the time used =  (n) Thus, we include back the comparison time in all Partition calls, Running time =  ( n + X ) Proof: Running time =  (n+X)

41 Homework Problems 7-1 Practice in home:7.1-2, 7.3-1, 7.4-3