Question

In: Computer Science

Find the median (middle value) of an array of numbers, which we will assume are all...

Find the median (middle value) of an array of numbers, which we will assume are all distinct. For example, if there are 27 elements, then you must return the 14th smallest (which is also the 14th largest). The strategy used to solve this problem is somewhat like the quicksort algorithm, but has some important differences. Consider choosing a pivot element and partitioning the input so that the elements less than the pivot are to its left, and those larger than the pivot to its right.


(a) Approximately how many comparisons are required to perform this partitioning?


(b) How can we efficiently determine which partition contains the median?


(c) Having answered part (b), describe a recursive algorithm to find the median.


(d) Now assume that you can guarantee that the pivot partitions the input into one part that has at most 3/4 of the elements, and one part that has at least 1/4 of the elements. Write a recurrence relation for the number of comparisons in this algorithm, C(n), assuming the worst partition, subject to the guarantee.


(e) Solve this recurrence relation, assuming C(1)=0. What do you conclude about the running time required to find the median element?


(f) How can choosing the pivot element uniformly at random help you to obtain the guarantee mentioned in part (d)?

Solutions

Expert Solution

a) In worst case we will require around N2 comparisons as partition function i.e. finding pivot element takes O(N) time and there can be O(N) pivot elements

Each pivot element takes O( N ) time so

No of comparisons = O (N) * O(N) = O(N2) = N2

b) We will apply partition algorithm and arrange all the elements smaller than the pivot on its left and the elements greater than it on its right.

Lets say pos of pivot is P .

Now the position of the chosen pivot is the middle of the array then it is the required median of the given array.

Let k = middle element of array  

and We are currently at Partition from L to R

If P >= k then find the index in first half of array

i,e apply partition algo on arr( L to P)

If P < K  then find the index in second half of array

i,e apply partition algo on arr( P to R)

c)

Algorithm

1. Randomly pick pivot element from arrray and the using the partition step from the quick sort algorithm arrange all the elements smaller than the pivot on its left and the elements greater than it on its right.

2. If after the previous step, the position of the chosen pivot is the middle of the array then it is the required median of the given array.

3. If the position is before the middle element then repeat the step for the subarray starting from previous starting index and the chosen pivot as the ending index.

4. If the position is after the middle element then repeat the step for the subarray starting from the chosen pivot and ending at the previous ending index.

5. In case of even number of elements, the middle two elements have to be found and their average will be the median of the array.

6. Repeat Step 3 or 4 until you find the median of array

d) Recurrence Relation

C(n) = C(n/4) + C(3n/4) + cn

here c is a constant

Note This Recurrence relation represents average time complexity of Quicksort Algorithm

e)

C(1) = 0

For Convenience lets denote C using T

T(n) = T(n/4) + T (3n/4) + cn here c is a constant

Drawing recurrence tree of above recurrence relation :

it takes log4/3N levels to get down to a subproblem of size 1.

Since each right child is 3/4 of the size of the node above it (its parent node), each parent is 4/3 times the size of its right child.

So from subproblem of size 1 we will multiply the size by 4/3 until we reach n.

Now for For what value of X (4/3)x = n

Taking log with base 4/3 to both sides  

we will get x = log4/3n .

there are log4/3n levels each level have partitioning time at most cn

So overall Time complexity = O(nlog4/3n)

This is concluded time complexity of finding the median.

f) We if choose pivot element uniformly and randomly then pivot element always occur in middle part of the array.

This can be showed by the below image

So choosing pivot element uniformly randomly will guarantee atmost 3/4 elements at one side  and 1/4 elements at other side.


Related Solutions

Suppose that all the numbers in an array are located in an interval [0,12], and we...
Suppose that all the numbers in an array are located in an interval [0,12], and we need to find the largest element with accuracy ε = 0.8. How many iterations will you need if we use the quantum optimization algorithm? How many times do we need to apply Grover's algorithm? Trace the quantum optimization algorithm for the case when the actual largest element is a5 = 3.14
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Suppose we are interested in whether there is a difference between the median numbers of hours...
Suppose we are interested in whether there is a difference between the median numbers of hours spent each week by men and woman watching television. Two random samples were taken: the numbers of hours taken by men and women watching television are shown below. You are about to test the null hypothesis that there is no difference in weekly television watching hours between men and woman. Men: 5 10 12 15; Women: 0 7 4 3 6 8 a) calculate...
Suppose we are interested in whether there is a difference between the median numbers of hours...
Suppose we are interested in whether there is a difference between the median numbers of hours spent each week by men and woman watching television. Two random samples were taken: the numbers of hours taken by men and women watching television are shown below. You are about to test the null hypothesis that there is no difference in weekly television watching hours between men and woman. Men: 5 10 12 15; Women: 0 7 4 3 6 8 a) calculate...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] =...
Project: Given an array numbers. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of numbers. Example: Input: nums = [1,2,3,4] Output: [1,3,6,10] Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. You need to do: Create a class called RunningSumArray to perform the main method. Create a class called ArrayOperationto hold the actions for array. In this project, you will need 4methods: 1. Public static Int[] getArray() --- inside ArrayOperationclass,...
How could we use Pthreads to find the minimum value in an array without mutex locks?...
How could we use Pthreads to find the minimum value in an array without mutex locks? Would this version be slower? If so, why?
We have an array of numbers, and we start at index 0. At every point, we're...
We have an array of numbers, and we start at index 0. At every point, we're allowed to jump from index i to i+3, i+4, or stop where we are. We'd like to find the maximum possible sum of the numbers we visit. For example, for the array [14, 28, 79, -87, 29, 34, -7, 65, -11, 91, 32, 27, -5], the answer is 140. (starting at the 14, we jump 4 spots to 29, then 3 spots to 65,...
implement in LEGV8 find the smallest value in an array.
implement in LEGV8 find the smallest value in an array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT