Question: Find a linear - time algorithm to determine, as efficiently as possible, the smallest and second smallest element in an array of n numbers. 1

Find a linear-time algorithm to determine, as efficiently as possible, the smallest and second smallest element in an array of n numbers. 1. After you show that the algorithm runs in linear time, establish eaxactly how many compari- son your algorithm executes in worst-case. So, we are interested in the constant looking only at comparisons though. 2. Analogously, the largest and second largest element in that array can be found. What would be your algorithm? 3. Alternately, if you dont want to write a new algorithm for 2., how could you use an imple- mentation of your algorithm from 1. to solve 2 by transforming the input appropriately and in linear time? 4. Now, using the above two algorithms (from 1. and 2.), you should be able to find the median of the elements stored in the array. How? What is the time complexity? 5. Next, using the concept of loop invariant, prove that this algorithm correctly finds the median of a set of n elements.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!