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 lineartime algorithm to determine, as efficiently as possible, the smallest and second smallest element in an array of n numbers. After you show that the algorithm runs in linear time, establish eaxactly how many compari son your algorithm executes in worstcase. So we are interested in the constant looking only at comparisons though. Analogously, the largest and second largest element in that array can be found. What would be your algorithm? Alternately, if you dont want to write a new algorithm for how could you use an imple mentation of your algorithm from to solve by transforming the input appropriately and in linear time? Now, using the above two algorithms from and you should be able to find the median of the elements stored in the array. How? What is the time complexity? 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
