Question

In: Computer Science

Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an...

Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an efficient algorithm for finding the median of all numbers in arrays X0 and X1. Now, suppose you have not two, but three such arrays, each with n elements. Devise and analyze an efficient algorithm for finding the median. Do the same for n arrays, each with n elements.

Solutions

Expert Solution

Median: To get median we need to sort array and find the middle element .that will be median

Steps to get the median

Consider array {6,3,7,8,1}

For the given array we need o sort the array first

Then the sorted array is{1,3,6,7,8}

Then median is middle element. So for the given example median is 6.

Consider the array are given below

X=[4,2,7]

Y=[1,8]

For finding median first we need to combine the two arrays.

Then the result is

Z=[1,2,4,7,8]

So the median is middle element. So 8 is the median

What I explained above is no of elements if its odd.
Suppose the total no of element is even what will be the median. Lets find out.

Consider the same arrays with addition of one more element 5 in Y.

So our array will look like

X=[4,2,7]

Y=[1,8,5]

For finding median first we need to combine the two arrays.

Then the result is

Z=[1,2,4,5,7,8]

Here we cannot choose the middle element. The total no of element is 6.so will pick

3rd and 4th element which 4 and 5 respectively and take the average of that.

Then that will be median.

Ie (4+5)/2=4.5

Now I am trying to introduce new algorithm which is efficient to sort 2 arrays.

Using median of one array set against other array median

Consider r1 and r2 are two arrays

-find out the median of r1 and r2.lets say the median of r1 as s1 and median of r2 as s2.

-suppose s1=s2 .then we found out median in first step itself.

-suppose s1!=s2 and s1>s2 then median will be in

From first no of r1 to s1 or will be in first no of r2 to s2

- suppose s1!=s2 and s2>s1 then median will be in

From last no of r1 to s1 or will be in last no of r2 to s2

-this procedure should repeat until the count become 2.

-when the count is 2 use the equation to find out median

We call median of 2 arrays as D

Then D=(LARGEST OF(S1[0],S2[0]+SMALLEST OF(S1[1],S2[1]))/2


Related Solutions

Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
Consider two sets of integers represented in arrays, X = [x1, x2, . . . ,...
Consider two sets of integers represented in arrays, X = [x1, x2, . . . , xn] and Y = [y1, y2, . . . , yn]. Write two versions of a FindSetUnion(X, Y ) algorithm to find the union of X and Y as an array. An element is in the union of X and Y if it appears in at least one of X and Y . You may make use any algorithm introduced in the lectures to...
You are given two arrays A1 and A2, each with n elements sorted in increasing order....
You are given two arrays A1 and A2, each with n elements sorted in increasing order. For simplicity, you may assume that the keys are all distinct from each other. Describe an o(log n) time algorithm to find the (n/2) th smallest of the 2n keys assuming that n is even.
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths...
Problem 1: You have two sorted lists of integers, L1 and L2. You know the lengths of each list, L1 has length N1 and L2 has length N2. Design a linear running time complexity algorithm (ONLY PSEUDOCODE) to output a sorted list L1 ∧ L2 (the intersection of L1 and L2). Important Notes: For this problem, you don’t need to submit any implementation in Java. Only the pseudocode of your algorithm is required. Pseudocode is a simple way of writing...
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n....
Let X1 and X2 be uniform on the consecutive integers -n, -(n+1), ... , n-1, n. Use convolution to find the mass function for X1 + X2.
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
A sequence of integers x1,x2,...,xn is unimodal if for some 1 ≤ i ≤ n we...
A sequence of integers x1,x2,...,xn is unimodal if for some 1 ≤ i ≤ n we have x1 < x2 < ... < xi and xi > xi+1 > ... > xn. Find the maximum element in a unimodal sequence of integers x1, x2, . . . , xn. The running time should be O(log n). Show that your algorithm meets the bound.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT