Question: Question 2 . ( 2 0 marks ) We want to compute the median of every prefix of an input array A [ 1 .
Question marks We want to compute the median of every prefix of an input array An of arbitrary integers. More precisely, design an algorithm that outputs another array M n so that M i is equal to the median of the integers in the subarray Ai Recall that when i is odd, the median of Ai is the element of rank i in the subarray, and when i is even, the median is the average of the elements with ranks i and i Your algorithm should run in worstcase time On log n Hint: Maintain two heaps for the subarray Ai: one that contains i elements of this subarray, and one that contains i elements of this subarray. What elements of Ai should each one of these heaps contain? What kind of heap each one them is How do you use them to compute the median of Ai How do you maintain these heaps when you increase i to i a Describe your algorithm in clear and concise English, and also provide the corresponding pseudocode. Argue that your algorithm is correct. b Justify why your algorithm runs in time On log n in the worst case.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
