/*! This file is auto-generated */ .wp-block-button__link{color:#fff;background-color:#32373c;border-radius:9999px;box-shadow:none;text-decoration:none;padding:calc(.667em + 2px) calc(1.333em + 2px);font-size:1.125em}.wp-block-file__button{background:#32373c;color:#fff;text-decoration:none} Q24E Question: On page 66 there is a ... [FREE SOLUTION] | 91影视

91影视

Question: On page 66 there is a high-level description of the quicksort algorithm.

(a) Write down the pseudocode for quicksort.

(b) Show that its worst - case running time on an array of size n is (n)2.

(c) Show that its expected running time satisfies the recurrence relation.

T(n)O(n)+1ni=1n-1(Ti+Tn-i)

Then, show that the solution to this recurrence is O(nlogn).

Short Answer

Expert verified

a).The pseudocode for quicksort is given below.

b). Its worst - case running time on an array of size n is(n2)is proved.

c).The solution has runtime of O(nlogn).

Step by step solution

01

Step – 1:Fast sorting Algorithm.

Fast sorting Algorithm:

鈥 Using a divide-and-conquer strategy, Quick Sort sorts the specified array of items.

鈥 Rapid sort randomly chooses a pivot element v. The specified input array is then separated into three sub-arrays: AL, AR, and Av. Where AL represents the sub-array of items lower than v, AR represents the subarray of elements bigger than v, and Av represents the sub-array of elements equal to v.

鈥 After that, Quick sort recursively sorts the two sub-arrays AL,AR.,

02

Step – 2:  Pseudocode for quicksort.

(a)

Pseudocode for sorting quickly:

selection sort is a quicksort function (A[1 ...n])

A[1...] is an array of items .n]

A sorted array A [1...] is the output .n]

v= A[k] chose k at random from 1,2,...,n

AL,AR,Av=split AL,AR,Av= (A[1 ...n],v)

quicksort(AL)

quicksort(AR),

03

Step – 3:The worst-case running time on an array.

(b)

鈥 In the worst-case scenario, rapid sort chooses the biggest member of the array A every time. That is, AR is an empty sub array every time. AL also has a maximum size of n-1.

鈥 As a result, the problem is simply reduced to a subproblem of size n-1 each time.

In the worst-case scenario, the quicksort algorithm will take the following amount of time to run:

T(n) = T(n-1) + O(n)

Expand the above recurrence

T(n) = T(n-2) +n-1+n

= T(n-3) + n -2 + n 鈥 1 + n

T(n) = 1+2+3+ .....+n-1+n

=nn+12

Therefore ,

T(n) = nn+12 鈮 c.n2 = O(n2) for some constant c > 0

T(n) = nn+12 鈮 c.n2 = 惟(n2) for some constant c > 0

Therefore, initial definition of bigT(n) = nn+12 = 惟(n2)

Therefore, the useless condition of running time of the quicksort on an array of size nisO(n2).

04

Step – 4:  The run time of solution.

(c)

鈥 Assume that pj is the probability that A[k] is the jth biggest element. 1 鈮 j 鈮 n

鈥 pj=1/n so that any component inside the array A could be the jth biggest.

鈥 Also, when the pivot element A[k] is the jth biggest element, suppose Tj is the estimated running time.

鈥 Then total assumption running time, Tn=j=1npjTj

鈥 When the pivot element A[k] is the jth largest, then ALhas at most n-j+1 and ARhas at most

j-1 elements.

鈥 ThusTj is at most T(n-j+1) + T(i-1) +O(n) .

Tn=j=1npjTjj=1nTn-j+1+Tj-1+On12j=1nTn-j+1+Tj-1+On12j=1nOn+12j=1nTn-j+1+Tj-1On+12j=1nTn-i+Ticn+12j=1nTn-i+Ti

Hence TnOn+1nj=0n-1Tn-i+Ti ------(1)

To show that T(n) = O(n log n), use induction.

Assume T(n) = O(n log n) implies T(n) 鈮 bn log n ------(2)

Re write equation(1) as follows:

Tncn+2nj=0n/2Tn-i+Ti

From inductive hypothesis(equation(2)),

Tncn+2bnj=0n/2n-ilogn-i+ilogi+j=0n/2n-ilogn-i+ilogiTncn+2bn[j=0n/2n-ilogn-i+ilogi

Again, re write above in equation as follows:

Tncn+2bn[j=1n/4n-ilogn-i+ilogi+j=0n/2n-ilogn-i+ilogi]

Therefore, by induction, it is proved that the solution has run time of Onlogn.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with 91影视!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Section 2.2 describes a method for solving recurrence relations which is based on analyzing the recursion tree and deriving a formula for the work done at each level. Another (closely related) method is to expand out the recurrence a few times, until a pattern emerges. For instance, let鈥檚 start with the familiar T(n)=2T(n/2)+o(n). Think of o(n) as being role="math" localid="1658920245976" <cnfor some constant , so: T(n)<2T(n/2)+cn. By repeatedly applying this rule, we can bound T(n) in terms of T(n/2), then T(n/4), then T(n/8), and so on, at each step getting closer to the value of T(.) we do know, namely .

T(1)=0(1).

T(n)2T(n/2)+cn2[2Tn/4+cn/2]+cn=4T(n/4)+2cn4[2Tn/8+cn/4]+2cn=8T(n/8)+3cn8[2Tn/16+cn/8]+3cn=16T(n/16)+4cn

.

.

.

A pattern is emerging... the general term is

T(n)2kT(n/2k)+kcn

Plugging in k=log2n, we get T(n)nT(1)+cnlog2n=0(nlogn).

(a)Do the same thing for the recurrence T(n)=3T(n/2)+0(n). What is the general kth term in this case? And what value of should be plugged in to get the answer?(b) Now try the recurrence T(n)=T(n-1)+0(1), a case which is not covered by the master theorem. Can you solve this too?

This problem illustrates how to do the Fourier Transform (FT) in modular arithmetic, for example, modulo .(a) There is a number such that all the powers ,2,...,6 are distinct (modulo ). Find this role="math" localid="1659339882657" , and show that +2+...+6=0. (Interestingly, for any prime modulus there is such a number.)

(b) Using the matrix form of the FT, produce the transform of the sequence (0,1,1,1,5,2) modulo 7; that is, multiply this vector by the matrix M6(), for the value of you found earlier. In the matrix multiplication, all calculations should be performed modulo 7.

(c) Write down the matrix necessary to perform the inverse FT. Show that multiplying by this matrix returns the original sequence. (Again all arithmetic should be performed modulo 7.)

(d) Now show how to multiply the polynomials and using the FT modulo 7.

In Section 1.2.3, we studied Euclid鈥檚 algorithm for computing the greatest common divisor (gcd) of two positive integers: the largest integer which divides them both. Here we will look at an alternative algorithm based on divide-and-conquer.

(a) Show that the following rule is true.

gcd(a,b)={2gcd(a2,b2)ifa,bareevengcd(ab2)ifaisodd,bisevengcd(a-b2,b)ifa,bareodd

(b) Give an efficient divide-and-conquer algorithm for greatest common divisor.

(c) How does the efficiency of your algorithm compare to Euclid鈥檚 algorithm if a and b are n-bit -bit integers? (In particular, since n might be large you cannot assume that basic arithmetic operations like addition take constant time.)

Suppose you are choosing between the following three algorithms: 鈥 Algorithm A solves problems by dividing them into five sub-problems of half the size, recursively solving each sub-problem, and then combining the solutions in linear time. 鈥

Algorithm B solves problems of size n by recursively solving two sub-problems of size n-1and then combining the solutions in constant time. 鈥 Algorithm C solves problems of size n by dividing them into nine sub-problems of size n/3, recursively solving each sub-problem, and then combining the solutions in O(n2)time.

What are the running times of each of these algorithms (in big- O notation), and which would you choose?

In this problem we will develop a divide-and-conquer algorithm for the following geometric task.

CLOSEST PAIRInput: A set of points in the plane, {p1=(x1;y1),p2=(x2,y2),...,pn=(xn,yn)}

Output: The closest pair of points: that is, the pair PiPjfor which the distance between piand pj, that is,

(xi-xi)2+z(yi-yi)2,

is minimized.

For simplicity, assume that n is a power of two, and that all the x-coordinates role="math" localid="1659237354869" xi are distinct, as are the y-coordinates.

Here鈥檚 a high-level overview of the algorithm:

.Find a value for which exactly half the points have xi<x, and half have xi>x. On this basis, split the points into two groups, L and R.

鈥 Recursively find the closest pair in L and in R. Say these pairs are pLqLLand pRqRRwith distances dLand dR respectively. Let d be the smaller of these two distances.

鈥 It remains to be seen whether there is a point in Land a point in R that are less than distance dapart from each other. To this end, discard all points with xi<x-dor xi>x+d and sort the remaining points by y-coordinate.

鈥 Now, go through this sorted list, and for each point, compute its distance to the seven subsequent points in the list. Let pMqMbe the closest pair found in this way.

鈥 The answer is one of the three pairs role="math" localid="1659237951608" {pL,qL},{pR,qR}{pM,qM}, whichever is closest.

(a) In order to prove the correctness of this algorithm, start by showing the following property: any square of size dd in the plane contains at most four points of L.

(b) Now show that the algorithm is correct. The only case which needs careful consideration is when the closest pair is split between L and R.

(c) Write down the pseudocode for the algorithm, and show that its running time is given by the recurrence:

T(n)=2T(nl2)+0(nlogn)

Show that the solution to this recurrence is o(nlogzn).

(d) Can you bring the running time down to O(nlogn)?

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.