# Quicksort Lecture 3 Prof. Dr. Aydın Öztürk. Quicksort.

## Presentation on theme: "Quicksort Lecture 3 Prof. Dr. Aydın Öztürk. Quicksort."— Presentation transcript:

Quicksort Lecture 3 Prof. Dr. Aydın Öztürk

Quicksort

Divide and Conquer

Partitioning Subroutine

Example of Partitioning

Running time for PARTITION The running time of PARTITION on the subarray A[p...r] is where n=r-p+1

Pseudo code for Quicksort

Analysis of Quicksort

Worst-case of quicksort

Worst-case decision tree

Worst-case analysis For the worst case, we can write the recurrence equation as We guess that

Worst-case analysis This expression achieves a maximum at either end points: q=0 or q=n-1. Using the maximum of T(n) we have Thus This expression achieves a maximum at either end points: q=0 or q=n-1.

Worst-case analysis We can also show that the recurrence equation as Has a solution of We guess that

Worst-case analysis Using the maximum of T(n) we have We can pick the constant c 1 large enough so that and Thus the worst case running time of quicksort is

Best-case analysis

Analysis of almost best-case

Best-case analysis For the best case, we can write the recurrence equation as We guess that

Best-case analysis

This expression achieves a minimum at Using the minimum of T(n) we have

More intuition

Randomized quicksort

R ANDOMIZED-PARTITION (A, p, r) 1i←RANDOM(p, r) 2exchange A[r]↔A[i] 3return PARTITION(A, p, r) RANDOMIZED-QUICKSORT (A,p,r) 1if p { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/13/4034233/slides/slide_43.jpg", "name": "R ANDOMIZED-PARTITION (A, p, r) 1i←RANDOM(p, r) 2exchange A[r]↔A[i] 3return PARTITION(A, p, r) RANDOMIZED-QUICKSORT (A,p,r) 1if p

Randomized quicksort

Randomized quicksort analysis

Calculating Expectation

Substitution Method

Calculating Expectation(Alternative approach) Example: Sort the following 10 distinct values: 5, 9, 3, 10, 11, 14, 8, 4, 17, 6 Pivot: 10 {5, 9, 3, 8, 4, 6}, 10, {11, 14, 17} Pivot: 6 {5, 3, 4}, 6, {9, 8}, 10, {11, 14, 17} Pivot: 4 {3}, 4, {5}, 6, {9, 8}, 10, {11, 14, 17}

Calculating Expectation(Alternative approach) Let X be the total number of comparisons performed in all calls to PARTITION. Let being the ith smallestelement of the array A. We define the set Let also

Calculating Expectation(Alternative approach) To determine the prob. that and are ever compared, we note that the values will initially in the same bracket and will remain in the same bracket if the number chosen for the first comparison is not between and. For example, if the comparison number is larger than, then all the values will go in a bracket to the left of the comparison number, and if it is smaller than, then they will all go in a bracket to the right. Thus all the values will remain in the same bracket until the first time that one of them chosen as a comparison value. At that point all the other values between i and j will be compared with this comparison value. If this comparison value is neither i or j then when compared with it, i will go into a left bracket and j will go into right bracket, consequently i and j will never be compared. If the comparison value of the set is either i or j then there will be a direct comparison between i and j.

Calculating Expectation(Alternative approach) The total number number of comparison performed by the algorithm

Calculating Expectation(Alternative approach) Taking expectation of both sides

Calculating Expectation(Alternative approach) Taking expectation of both sides

Calculating Expectation(Alternative approach)

Quicksort in practice