Question

In: Computer Science

Let An = {ai} n i=1 denote a list of n distinct positive integers. The median...

Let An = {ai} n i=1 denote a list of n distinct positive integers. The median mA of An is a value in An such that half the elements in An are less than m (and so, the other half are greater than or equal m). In fact, the median element is said to have a middle rank. (a) Develop an algorithm that uses Sorting to return mA given An. (6%) (b) Now assume that one is given another list Bn = {bi} n i=1 of n distinct positive integers whose median mB is already known. Develop an algorithm that returns the sum of the two elements with value closest to mB, such that one of them is greater than mB and the other is lower than mB. Although sorting Bn would yield a quick solution, we will see later on that this is a exhorbitantly slow process and one can solve the problem without sorting Bn in singnificantly faster time. Hence, in your solution, Do Not Sort Bn and propose a solution that goes without Sorting. (6%) (c) State a loop invariant for the algorithm you proposed in part (b) above.(6%) (d) Prove the loop invariant you proposed in part (c) above

Solutions

Expert Solution

please please thumbs up!!!

hope it will help uh out!!!

Solution::
(TYPED)
-------------------------------------------

Answer A:: ALGORITHMS

Algorithm partition (An, low, high)

    Input: a list of n integers and lowest & highest indexes

    Output: partitioning index

    pivot ← A[high]

    i ← low – 1

  for j = low to high-1

         if A[j] < pivot

            i ← i + 1

           swap A[i] and A[j]

swap A[i+1] and A[high]

   return (i+1)

Algorithm quicksort (An, low, high)

    Input: a list of n integers and lowest & highest indexes

    Output: nothing

       if low < high

         pi ← partition (An, low, high)

         quicksort (An, low, pi-1)

         quicksort (An, pi+1, high)

Algorithm median (An)

Input: a list of n integers

Output: median of the data

    quicksort (An, 0, n-1)

if n%2 ≠ 0

         return A[n/2]

   else

         return (A[n/2]+A[(n/2)+1])/2

---------------------------------------------------------

Answer :: B

Algorithm find_pair (An, mB)

    Input: a list of n integers and its median

    Output: the two required numbers

    x ← -INF/2

     y ← +INF/2

for i = 0 to n-1

      for j = 0 to n-1

       if A[i]< mB and A[j]> mB and |mB-(A[i]+A[j])| < |mB –(x+y)|

                 x = A[i]

                 y = A[j]

    return (x,y)

---------------------------------------------------

Answer :: (C)

Here In Part (B)  It is true for every iteration that x < mB and y > mB.

--------------------------------------

Answer :: (D)

Since, x and y are initialized to minus infinity and infinity respectively and they get changed to A[i] and A[j] respectively, in each iteration of the loop if at least both A[i] < mB & A[j] >mB are true, therefore both loop invariants should also be true.


Related Solutions

Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum...
Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum of the positive divisors of n (as in the notes). (a) Evaluate τ (1500) and σ(8!). (b) Verify that τ (n) = τ (n + 1) = τ (n + 2) = τ (n + 3) holds for n = 3655 and 4503. (c) When n = 14, n = 206 and n = 957, show that σ(n) = σ(n + 1).
Let Dn be the set of positive integers that divide evenly into n. List the elements...
Let Dn be the set of positive integers that divide evenly into n. List the elements of each of the sets D6, D16, D12, and D30
(i) T(n) denote the number of distinct ways that a postage of n cents, where n...
(i) T(n) denote the number of distinct ways that a postage of n cents, where n ≥ 4 and n is even, can be made by 4-cent and 6-cent stamps. Find a recurrence relation T(n). NOTE [4,6] is the same as [6,4] so T(10) = 1 so T(n) is NOT T(n-4)+T(n-6) (ii) Now assume we have 10-cent stamps in addition to the previous 2 kinds. Find a recurrence relation, S(n), for the number of distinct ways that a postage of...
(i) T(n) denote the number of distinct ways that a postage of n cents, where n...
(i) T(n) denote the number of distinct ways that a postage of n cents, where n ≥ 4 and n is even, can be made by 4-cent and 6-cent stamps. Give a recursive algorithm (written in Python) to compute T(n) for n ≥ 4 and n is even. Briefly explain why the algorithm is correct but no formal proof is required. NOTE [4,6] is the same as [6,4] so T(10) = 1. (ii) Now assume we have 10-cent stamps in...
Let us choose seven arbitrary distinct positive integers, not exceeding 24. Show that there will be...
Let us choose seven arbitrary distinct positive integers, not exceeding 24. Show that there will be at least two subsets chosen from these seven numbers with equal total sums. (Keep in mind that sets, and hence subsets, have no repeated elements.) Hint: How many subsets can you form altogether? What is the largest total sum of such a subset?
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct...
Let S{a, b, c, d} be a set of four positive integers. If pairs of distinct elements of S are added, the following six sums are obtained:5,10, 11,13,14,19. Determine the values of a, b, c, and d. (There are two possibilities. )
. Let xj , j = 1, . . . n be n distinct values. Let...
. Let xj , j = 1, . . . n be n distinct values. Let yj be any n values. Let p(x) = c1 + c2x + c3x 2 + · · · + cn x ^n−1 be the unique polynomial that interpolates the data (xj , yj ), j = 1, . . . , n (Vandermonde approach). (a) Remember that (xj , yj ), j = 1, . . . , n are given. Derive the n...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Let E(n) denote the expected return on asset i and B. denote the corresponding beta. In addition, let E(rm) denote the expected
Let E(n) denote the expected return on asset i and B. denote the corresponding beta. In addition, let E(rm) denote the expected return on the market portfolio and Bm denote the corresponding beta. Define and sketch the Security Market Line (SML). Hint: Use E(rm) - r = 8%, r = 3%, B. = 1.25 and B2 = 0.6.
A certain system can experience three different types of defects. Let Ai (i = 1,2,3) denote...
A certain system can experience three different types of defects. Let Ai (i = 1,2,3) denote the event that the system has a defect of type i. Suppose that P(A1) = 0.29, P(A2) = 0.27, P(A3) = 0.36, P(A1 ∪ A2) = 0.52, P(A1 ∪ A3) = 0.57, P(A2 ∪ A3) = 0.58, P(A1 ∩ A2 ∩ A3) = 0.01 (a) Find the probability that the system has exactly 2 of the 3 types of defects. (b) Find the probability...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT