in Algorithms retagged by
316 views
0 votes
0 votes
We've been given an unordered list having n distinct elements,the no. Of comparison to find an element that is neither the 2nd minimum nor the 2nd maximum is?
in Algorithms retagged by
316 views

1 Answer

1 vote
1 vote
To find an element that is neither the 2nd minimum nor the 2nd maximum, We can pick any five elements from the array and sort them...the middle element of these five elements will be guaranteed to be a number which is neither the 2nd minimum nor the 2nd maximum,

Hence, let's pick the first five elements of the array and apply Insertion sort on them. We will, in worst case, have to make $10$ comparisons. And time complexity of this algorithm will be $O(1)$.

1 comment

What if we try not to choose any 5elements ,instead we form a heap and comparing?
0
0

Related questions

0 votes
0 votes
3 answers
3