Question

2. (10) Given a ”black-box” worst-case linear-time median subroutine, give a simple, linear-time algorithm that solves...

2. (10) Given a ”black-box” worst-case linear-time median subroutine, give a simple, linear-time algorithm that solves the selection problem for any given k, where 1 ≤ k ≤ n. Once again, an algorithm should be presented first, followed by its analysis.

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

Let's Start with the objective, we want to find the "kth smallest" element in a array.

We have a function which can in linear time find the median. Now the algorithm goes as follows.

Step 1: Break the array into groups of 5 elements (a odd number) and get the median of each of these items i.e. total N/5 medians and put in a separate array

Step 2: Find the median of the array of all medians, that will be med_of_medians let's say.

Step 3: If we have N/10 medians smaller then med_of medians then we have N/10 * 3 elements smaller then the med_medians ( in worse case)

Step 4:

Case1- if the subset of items, smaller then . med_of_medians have more then "k" do the same . operations as Step 1-3 on them.

Case 2- If those numbers are less then "k" numbers then

. delete those numbers (let's say m numbers) and

make k=k-m. And do 1-3 for the remaining

Case 3- If there are exactly K number then the biggest

. number in that set of numbers is the required item.

Explaination -

We divide the array into "N/5" part and can do with 7/9/11 anything for the same.

And then make a median of all these median called med_of_ median.

Now this med of median, will be greater then N/10 items in this case and so we'll have all the numbers smaller then the Medians who themselves are smaller then med_median so we got in this case the smallest N/10x(3) if we include the median numbers in the array, and if we want Smallest K element

Then there is only 3 possibly

K in larger then this number so we delete this and find another group of smallest number and make k=k-m

Else k is smaller on which we try to find a smaller group of smallest number by same process we found this one

Third and the base case we got exactly K items in that case the biggest on this group is the kth item ( and we don't need to sort for that also) it's the median right before the med_if_median to its left.

Hope it helps, if any doubts put in comments

Add a comment
Know the answer?
Add Answer to:
2. (10) Given a ”black-box” worst-case linear-time median subroutine, give a simple, linear-time algorithm that solves...
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
  • Give an algorithm with the following properties. • Worst case running time of O(n 2 log(n))....

    Give an algorithm with the following properties. • Worst case running time of O(n 2 log(n)). • Average running time of Θ(n). • Best case running time of Ω(1).

  • problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to...

    problem 2 can use Det-Selection(A, p, q, r) as a sub-routine (i.e, you don't need to write its pseudo-code). To sort an array A, you will then call Det-QuickSort(A, 1, n). You also need to provide the worst case time complexity analysis of your algorithm. 2. (20 points) Given a set of n distinct numbers, we wish to find the k largest in sorted order using a comparison-based algorithm. Give an algorithm that implements each of the following methods, and...

  • (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an...

    (1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....

  • Hello I have an automata question could you help me? [1Points] Give a formal description of a Tu...

    Hello I have an automata question could you help me? [1Points] Give a formal description of a Turing Machine M  that takes two parameters: an integer and an array of integers and decides whether the given integer is an element of the array or not. You can assume that all the integers are between 0 and 9. The input string will be written on the tape of the Turing machine. The first square of the tape contains the integer, the...

  • 1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark...

    1. [5 marks Show the following hold using the definition of Big Oh: a) 2 mark 1729 is O(1) b) 3 marks 2n2-4n -3 is O(n2) 2. [3 marks] Using the definition of Big-Oh, prove that 2n2(n 1) is not O(n2) 3. 6 marks Let f(n),g(n), h(n) be complexity functions. Using the definition of Big-Oh, prove the following two claims a) 3 marks Let k be a positive real constant and f(n) is O(g(n)), then k f(n) is O(g(n)) b)...

  • Introduction Many doors in public places are fitted with simple and inexpensive mechanisms that are designed...

    Introduction Many doors in public places are fitted with simple and inexpensive mechanisms that are designed to close the doors while reducing the slamming force to a minimum. Figure 1 Figure 1 depicts one such mechanism while Figure 2 provides a schematic description. The spring is mounted in order to push the door back to close once it is opened. The role of the hydraulic dashpot is to provide a viscous force that will prevent the door from slamming against...

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • Item 1 In the case below, the original source material is given along with a sample...

    Item 1 In the case below, the original source material is given along with a sample of student work. Determine the type of plagiarism by clicking the appropriate radio button. Original Source Material Student Version In the 1986 soccer World Cup final, the Argentine star Diego Maradona did not score a goal but his passes through a ring of West German defenders led to two Argentine goals. The value of a star cannot be assessed only by looking at his...

  • John Beckett enjoys vegetables, so much so that he has given up his full-time job as...

    John Beckett enjoys vegetables, so much so that he has given up his full-time job as a lawyer to concentrate on growing and marketing organic vegetables. He started growing vegetables 20 years ago in his back garden and eventually became fully self-sufficient in supplying vegetables for the family. Partly bored with his legal job and tempted by an attractive severance package, John decided he would try to establish his own vegetable supply business. Eighteen months ago he looked around for...

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