Question: 7 Bonus! Analyzing quickSort In this bonus section you will work through the analysis of the expected running time of everybody's favorite sorting algorithm: quickSort.

7 Bonus! Analyzing quickSort
In this bonus section you will work through the analysis of the expected running time of everybody's favorite
sorting algorithm: quickSort. Here is some pseudocode.
Input: An integer array A of length n. Assume for simplicity that every integer in A is unique.
Base Case: If n=1, return A.
Choose the Pivot: Choose a random index i{0,1,dots,n-1} and let p=A[i].
Filter Around the Pivot: Let s.t.x>pA0Ap;A1AB0=(A0)B1=(A1)B=B0+[p]+B1-e.g.,[12]+[34]=[1234].TnAnTn=O(nlogn)p(k+1)ABB[k]A0kA1n-k-1Tk+Tn-k-1p(k+1)-A1nk=0,dots,n-11nk=0n-1(Tk+Tn-k-1)O(1)O(n)TnTn=cn+1nk=0n-1(Tk+Tn-k-1).T1=1n*Tn=cn2+2k=0n-1Tkn*Tn=c(2n-1)+(n+1)*Tn-1Tnn+12cn+1+Tn-1n.Tnn+12c(1n+1+1n+cdots+13)+121+12+13+cdots+1n=O(logn)Tn=O(nlogn)x and A1=[xinAs.t.x>p].
SoA0 consists of all elements in A which are less than p;A1is all elements in A which are greater.
Recursive calls: Let B0= quickSort (A0) and B1= quickSort (A1).
Output: Return B=B0+[p]+B1, where here '+' denotes array concatenation
-e.g.,[12]+[34]=[1234].
Computing the runtime of quickSort is conceptually very similar to the runtime computation of mergeSort,
however complications arise because the pivot is chosen randomly. Let Tn denote the expected running time
of quickSort when the input array A has size n. Complete the following outline proving that Tn=O(nlogn).
(a)(Nothingto prove here) Suppose that pis the (k+1)-th smallest element inA,so that the pivot
appears inBatB[k].In this case, the length ofA0isk and the length ofA1isn-k-1, and so
the expected runtime of the recursive calls made in Step 4isTk+Tn-k-1. Because the pivot was
chosen randomly, the probability that pis indeed the (k+1)-th smallest element inAis1n for
all k=0,dots,n-1. Therefore, the expected runtime of the recursive calls is1nk=0n-1(Tk+Tn-k-1).
Finally, choosing the pivot takes time O(1) and the filter step takes time O(n). Thus, Tn satisfies:
Tn=cn+1nk=0n-1(Tk+Tn-k-1).
This will be our starting point. Also, note T1=1.
(b) Prove n*Tn=cn2+2k=0n-1Tk.
(c) Deduce n*Tn=c(2n-1)+(n+1)*Tn-1.
(d) Deduce
Tnn+12cn+1+Tn-1n.
(e) Now iterate this bound to get Tnn+12c(1n+1+1n+cdots+13)+12.
(f) Finally use the fact from calculus: 1+12+13+cdots+1n=O(logn)to get Tn=O(nlogn).
 7 Bonus! Analyzing quickSort In this bonus section you will work

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!