/*! 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

How many lines, as a function of n (in (.)form), does the following program print? Write a recurrence and solve it. You may assume is a power of . function f (n) if n > 1:

print_line (鈥樷榮till going鈥欌)

f (n/2)

f (n/2)

An array A [1...n] is said to have a majority element if more than half of its entries are the same. Given an array, the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The elements of the array are not necessarily from some ordered domain like the integers, a A2 nd so there can be no comparisons of the form 鈥渋s A[i]>A[j]?鈥. (Think of the array elements as GIF files, say.) However you can answer questions of the form: 鈥渋s ..?鈥 in constant time.

(a) Show how to solve this problem in O(nlog n) time. (Hint: Split the array A into two arrays A1 and of half the size. Does knowing the majority elements of A1 and A2 help you figure out the majority element of A? If so, you can use a divide-and-conquer approach.)

(b) Can you give a linear-time algorithm? (Hint: Here鈥檚 another divide-and-conquer approach:

  • Pair up the elements of A arbitrarily, to get n/2 pairs
  • Look at each pair: if the two elements are different, discard both of them; if they are the same, keep just one of them
    Show that after this procedure there are at most n/2 elements left, and that they have a majority element if A does.)

Thesquare of a matrix A is its product with itself, AA.

(a) Show that five multiplications are sufficient to compute the square of a 2 x 2 matrix.

(b) What is wrong with the following algorithm for computing the square of an n x n matrix?

鈥淯se a divide-and-conquer approach as in Strassen鈥檚 algorithm, except that instead of getting 7 subproblems of size n2, we now get 5 subproblems of size n2 thanks to part (a). Using the same analysis as in Strassen鈥檚 algorithm, we can conclude that the algorithm runs in time O (nc) .鈥

(c) In fact, squaring matrices is no easier than matrix multiplication. In this part, you will show that if n x n matrices can be squared in time S(n) = O(nc), then any two n x n matrices can be multiplied in time O(nc) .

  1. Given two n x n matrices A and B, show that the matrix AB + BA can be computed in time 3S(n) + O(n2 ) .
  2. Given two n x n matrices X and Y, define the 2n x 2n matrices A and B,L as follows:
    A=X000andB=0Y00
    What is AB + BA, in terms of X and Y?
  3. Using (i) and (ii), argue that the product XY can be computed in time 3S(2n) + O(n2 ). Conclude that matrix multiplication takes time O(nc ).

Question: You are given an infinite array A[]in which the first n cells contain integers in sorted order and the rest of the cells are filled with . You are not given the value of n. Describe an algorithm that takes an integer x as input and finds a position in the array containing x, if such a position exists, in O(log n) time. (If you are disturbed by the fact that the array A has infinite length, assume instead that it is of length n, but that you don鈥檛 know this length, and that the implementation of the array data type in your programming language returns the error message whenever elements A[i]withi>n are accessed.)

An array A[1...n] is said to have a majority element if more than half of its entries are the same. Given an array, the task is to design an efficient algorithm to tell whether the array has a majority element, and, if so, to find that element. The elements of the array are not necessarily from some ordered domain like the integers, and so there can be no comparisons of the form 鈥 is A[i]>A[j] ?鈥. (Think of the array elements as GIF files, say.) However you can answer questions of the form: 鈥渋s ..?鈥 in constant time.

(a) Show how to solve this problem in O(nlogn) time. (Hint: Split the array A into two arrays A1and A2of half the size. Does knowing the majority elements of A1and A2help you figure out the majority element of A? If so, you can use a divide-and-conquer approach.)

(b) Can you give a linear-time algorithm? (Hint: Here鈥檚 another divide-and-conquer approach:鈥 Pair up the elements of A arbitrarily, to get n/2 pairs鈥 Look at each pair: if the two elements are different, discard both of them; if they are the same, keep just one of them . Show that after this procedure there are at most n/2 elements left, and that they have a majority element if A does.)

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.