Finding largest and 2 nd 2^\text{nd} largest number in a list

What is the minimum number of pairwise comparisons required to find the maximum and 2 nd 2^\text{nd} maximum number in a list of 128 numbers?


The answer is 133.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Ossama Ismail
Dec 14, 2016

Complexity of this algorithm is n + l o g 2 ( n ) 2 n + log_2(n) - 2

  • minimum # of comparisons = 128 + 7 2 = 133 = 128 + 7 - 2 = 133
  • The 128 numbers are divided into two sets of 64 each, perform pairwise comparison keep minimum set of numbers
  • The 64 numbers are divided into two sets of of 32 .....
  • ....... and so on till you reach to 4

  • the above steps requires 64 + 32 + 16 +8 +4 + 2 = 126 comparisons to get the maximum, = n 1 = n -1

To get the second maximum number, you need to check only those elements which were compared with the maximum number in the above part. So to find the 2nd maximum you need l o g 2 ( n ) 1 log_2(n) - 1

total = n + l o g 2 ( n ) 2 n + log_2(n) - 2

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...