/*! 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} Q15E In our median-finding algorithm ... [FREE SOLUTION] | 91影视

91影视

In our median-finding algorithm (Section 2.4), a basic primitive is the split operation, which takes as input an array S and a value V and then divides S into three sets: the elements less than V , the elements equal to V , and the elements greater than V . Show how to implement this split operation in place, that is, without allocating new memory.

Short Answer

Expert verified

The split will be done in place and with time bound:n

Step by step solution

01

Algorithm

Algorithms to performing "split" operations without any need for additional memory:

function splita1,...,n,v

hira=1fori=1ton:ifai<v:swapaiandahirahira=hira+1fori=hiraton:ifaiv:swapaiandahirahira=hira+1

02

Explanation of Algorithm

鈥 鈥渟plit 鈥 is the function which accepts the array 鈥s1,...,n鈥 and a value 鈥溾 as the input parameters.

鈥 Initially, the variable 鈥 count鈥 is assigned with the value.

鈥 The function has 2 鈥渇or 鈥 loops. In the first 鈥 for鈥 loop,

o Process all the elements in the array and brings the elements that are smaller than the value 2 鈥 鈥 to front of the array by swapping.

o Thus, the array is split into sub-array by moving all the smaller elements in the array to front.

鈥 It first checks if the value in the k鈥渢h鈥 element of the array is less than the value of 鈥 val鈥.

鈥 If it is true, it swaps the k鈥渢h鈥 element of the array 鈥 s鈥 with the position of the array element which has the value of count.

鈥 Then, the value of the 鈥渃ount鈥 is incremented.

鈥 In the second 鈥渇or鈥 loop,

o Find the position of the value 鈥 val鈥 and move it next to the sub-array by swapping.

鈥 It first checks if the value in the k鈥渢h鈥 element of the array is equal to the value of 鈥渧al 鈥.

鈥 If it is true, it swaps the k鈥渢h鈥 element of the array 鈥 s鈥 with the position of the array element which has the value of count.

鈥 Then, the value of the 鈥 count鈥 is incremented.

o Here, both the 鈥 for鈥 loops require constant time.

Therefore, it takes the running time of n.

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

Show that for any positive integers n and any base b , there must some power of b lying in the range [b,bn].

In Section 2.1 we described an algorithm that multiplies two n-bit binary integers x and y in time na, where a=log23. Call this procedure fast multiply (x,y).

(a) We want to convert the decimal integer 10n(a 1 followed by n zeros) into binary. Here is the algorithm (assume n is a power of 2):

function pwr2bin(n)

if n = 1: return10102

else:

z= ???

return fastmultiply(z,z)

Fill in the missing details. Then give a recurrence relation for the running time of the algorithm, and solve the recurrence.

(b) Next, we want to convert any decimal integer x with n digits (where n is a power of 2) into binary. The algorithm is the following:

function dec2bin(x)

if n=1: return binary [ x ]

else:

split x into two decimal numbers xt,xRwith n/2 digits each

return ???

Here binary [.] is a vector that contains the binary representation of all one-digit integers. That is, binary role="math" localid="1659333641173" [0]=02, binary [1]=12, up to binary [9]=10012. Assume that a lookup in binary takes 0(1) time. Fill in the missing details. Once again, give a recurrence for the running time of the algorithm, and solve it.

A kway merge operation. Suppose you have ksorted arrays, each with nelements, and you want to combine them into a single sorted array ofkn elements.

(a)Here鈥檚 one strategy: Using the merge procedure from Section 2.3, merge the first two arrays, then merge in the third, then merge in the fourth, and so on. What is the time complexity of this algorithm, in terms of kand n?

(b) Give a more efficient solution to this problem, using divide-and-conquer.

Practice with the fast Fourier transform.

(a) What is the FFT of (1,0,0,0)? What is the appropriate value of in this case? And of which sequence is (1,0,0,0)the FFT?

(b)Repeat for (1,0,1,-1).

You are given an array of nelements, and you notice that some of the elements are duplicates; that is, they appear more than once in the array. Show how to remove all duplicates from the array in time 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.