Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

minimum number of comparisons needed

what is the minimum number of comparisons needed to find the largest element from 4 distinct elements? I know for 5 distinct numbers it is 6, floor(5/2) * 3; this is from clrs book. but I know there is no one general formula for finding this, or is there?

edit clarification

these 4 elements could be in any different order(for all permutations of these 4 elements) im not interested in a counting technique to keep track of the largest element as you traverse the elements, but comparisons like > or <.

like image 699
David Avatar asked Dec 13 '25 01:12

David


2 Answers

for 4 elements the min. number of comparisons is 3.

In general, to find largest of N elements you need N-1 comparisons. This gives you 4 for 5 numbers, not 6.

Proof:

  1. there is always a solution with N-1 comparisons: just compare first two and then select the larger and compare with next one, select the larger and compare with next one etc....

  2. there cannot be shorter solution because this solution would not compare all the elements.

QED.

like image 86
Tomas Avatar answered Dec 14 '25 18:12

Tomas


I know it does not answer the original question, but I enjoyed reading this not-so-intuitive post on the minimum number of comparisons needed to find the smallest AND the largest number from an unsorted array (with proof).

like image 24
Franck Dernoncourt Avatar answered Dec 14 '25 20:12

Franck Dernoncourt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!