Question

Please show clear explanation for both parts explain how you would use merge sort for (a)...

Please show clear explanation for both parts explain how you would use merge sort for (a) and divide and conquer for (b).

Order Statistic. Given two sorted lists x and y. Both x and y have size n.

(a) Describe an algorithm that finds the median of X \bigcup Y in O(n) time.

(b) Describe an algorithm that finds the median of X \bigcup Y in O(logn) time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a)

This can be done by the following steps:

  1. First use the merge procedure on lists x and y. This takes O(n) time.
  2. Now we know that the merged list has 2n number of elements.
  3. Since, we have even number of elements, so median = mean of the two middle elements.
  4. The middle elements are in position - n and n+1
  5. Find the elements at n and n+1. This takes O(n) time because we have to traverse the list once.
  6. Average the elements found in previous step. This takes O(1) time because we are averaging 2 elements.
  7. The average found in prevoius step is the final answer.

Total time complexity = O(n) + O(n) + O(1) = O(n)

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

(b)

This can be done by repeatedly finding median of each list and comparing them which causes the lists to shrink.

Below are the steps:

  1. Find median of list1 and list2 as m1 and m2.
  2. Compare m1 with m2
  3. If m1 = m2, then return one of those.
  4. If m1 > m2
    • median must be between first element of list1 to m1, OR
    • from m2 to last element of list2
    • Shrink the lists appropriately. This will get rid of half of the elements.
  5. Similarly, process the lists if m2 > m1
  6. This is done till size of each list is 2.
    • Required median = maximum(list1[0], list2[0]) + maximum(list1[1], list2[1])/2

Since at each each recursive call, the size of elements is halved, therefore, time complexity is O(log n).

If you have any query, ask in the comment section. If helpful, click on the thumbs up button.

Add a comment
Know the answer?
Add Answer to:
Please show clear explanation for both parts explain how you would use merge sort for (a)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that...

    Lab Overview: In this lab, you will work with merge sort. Objectives: Identify a problem that can efficiently be solved with merge sort. Implement the merge sort algorithm Determine and summarize the algorithmic run-time of the merge sort algorithm. Specifications: As you saw from your readings and exercises, Merge Sort is based upon the idea of divide-and-conquer. If implemented correctly, your merge sort algorithm should have a time complexity of O(n*log(n)). Your task for this lab is to write a...

  • Note: when a problem has an array of real numbers, you cannot use counting sort or...

    Note: when a problem has an array of real numbers, you cannot use counting sort or radix sort. ​​​​​​​ For this problem, you are given a number t and an array A with n real numbers that are not sorted. Describe an algorithm that finds the t numbers in A that are closest to the median of A. That is, if A = {x1, . . . , xa) and xrn is the median, we want to find the t...

  • Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into...

    Consider a variation of Merge sort called 4-way Merge sort. Instead of splitting the array into two parts like Merge sort, 4-way Merge sort splits the array into four parts. 4-way Merge divides the input array into fourths, calls itself for each fourth and then merges the four sorted fourths. a)Implement 4-way Merge sort from Problem 4 to sort an array/vector of integers and name it merge4. Implement the algorithm in the same language you used for the sorting algorithms...

  • 6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into...

    6 6. Merge Bubble Sort: a) How does the merge bubble sort break the array into sublists? b) What does it need to use for each sublist and then what does it do with all of the sublists? c) What is the Big-O notation for this sort? 7. Merge Sort: a) How does the merge sort use recursion to break the array into sublists? b) What happens to each of these sublists to get the final sorted list? c) What...

  • please check my answers if it wrong answer me a) (25 points) Suppose that you are...

    please check my answers if it wrong answer me a) (25 points) Suppose that you are asked to analyze the performance. Algorithms operate on 1D array of size nor 2D a of the algorithms below, write down the Big O or order of grow terms of n. If the algorithm cannot be applied to the array, write 0(1), O(log n), O(n), O(n logn), 90), O(n"), O(n!). The will only be given for reasonable Big O answers. & algorithms for their...

  • the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++...

    the question from the course COMP 4040 that Analysis of Algorithms if you want to answer it by code please use C or C++ 5. Algorithm Design (20 points) Input: array A contains n distinct numbers from 1 to n, in arbitrary order. Output: number of inversions (defined as the number of pair(i, j) of array indices with i < j and A[i] > Aj]) (a) (5 points) What array with elements from the set {1, 2, ..., n) has...

  • A. What is the time complexity of Merge Sort? B. Explain why Merge Sort has the...

    A. What is the time complexity of Merge Sort? B. Explain why Merge Sort has the time complexity you listed above in Part A. Algorithm Quicksort (A, left, right) if (left < right) pivot Point = [(left+right)/2] 11 note central pivot i left - 1 ja right + 1 do do it i +1 while (i < A.size) and (A[i] SA[pivot Point]) do jj-1 while (j > i) and (A[il > A[pivot Point]) if (i <j) then swap (A[i], Aljl)...

  • 2. Here is a sorting algorithm that I like to use. Given an unsorted list of...

    2. Here is a sorting algorithm that I like to use. Given an unsorted list of size n, let Xx represent the data in location k of the unsorted list. Compare xi to X2 and switch if necessary so that they are in sorted order, smallest first. Compare Xn-1 and Xn and switch if necessary so that they are in sorted order, smallest first. • Compare x3 with its left neighbors, switching if necessary so that the 3 first entries...

  • You are given an array A of integers in sorted order. However, you do not know...

    You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...

  • I already solved part A and I just need help with part B 1. Matrix Multiplication...

    I already solved part A and I just need help with part B 1. Matrix Multiplication The product of two n xn matrices X and Y is a third n x n matrix 2 = XY, with entries 2 - 21; = xixYk x k=1 There are n’ entries to compute, each one at a cost of O(n). The formula implies an algorithm with O(nº) running time. For a long time this was widely believed to be the best running...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT