Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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).
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Multidimensional Array Data Management In Databases

Authors: Florin Rusu

1st Edition

1638281483, 978-1638281481

More Books

Students also viewed these Databases questions

Question

Discuss what we know about other species capacity for language.

Answered: 1 week ago

Question

How to reverse a Armstrong number by using double linked list ?

Answered: 1 week ago

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago