The optimal algorithms for finding the maximum and minimum numbers, and the minimum number of comparisons required for finding the two largest numbers

We know that looking for a maximum number in a data set with a capacity of n, no matter what comparison algorithm is used, it should be at least n-1 times, even if it is sorted by a competition, it will be n-1 times, otherwise, you cannot guarantee the maximum number. So what is the minimum number of comparisons between the maximum number and the minimum number in a data set with a capacity of n?

The Optimal Method for Finding the maximum and minimum numbers simultaneously from a data set with a capacity of n is: first, let all the elements participate in the two-to-one comparison, so that a total of n/Two comparisons are made, the maximum number must be in the winner group, and the minimum number must be in the loser group. Then, find the maximum number from the winner group with the capacity of n/2, compare n/2-1 times at least. Similarly, find the minimum number from the loser group with the capacity of n/2, and compare n/2-1 times at least. Therefore, we need to compare (3n/2) to 2 times in total. Assume n is an even number. The same is true for odd numbers.

This is the optimal algorithm for finding both the maximum and minimum numbers.

So how many times do we need to compare the second largest element from a data set with a capacity of n (assuming that the data set is a set without the same elements?

One habit is to first find the largest element, which needs to be compared n-1 times, and then find the largest element from the remaining n-1 elements, this element is the second element we are looking for, which requires comparing N-2 times. Make a total of 2n-3 times.

However,

There is also a better method:

(1) we are considering the comparative method of the knockout round. After the knockout round is over, we need to compare the number of the Champions. As shown in, we need to compare the number of 12 to 7.

(2) At this point, we should consider that runner-up should exist in those contestants who fail to win the championship (otherwise, each element has at least two elements larger than it ), because the number of elements that have been compared with the champion is ┌ log2n ratio, the maximum value of these elements needs to be compared to ┌ log2n ratio-1. As shown in, runner-up should be, 4. Otherwise, if the runner-up is 5, the champion 12 is bigger than it, and the 10 that has been compared with it is also bigger than it. At least two elements are greater than 5, so 5 is definitely not a candidate for the runner-up.

(3) To find out the runner-up to compare n-1 + batch log2n batch-1 = n-2 + batch log2n batch comparison. This algorithm is the best Algorithm for Finding runner-up.

Algorithm for Finding the maximum and minimum numbers

In programming

First, set a variable a, copy the first number to a, and compare them with the subsequent numbers one by one. As long as the number is larger than a, the value is assigned to. Always compare to the last one. At this time, a is the largest.

Optimal Algorithms for maximum and minimum values

This is impossible. Consider the three elements a, B, and c.

To find the maximum value, you must compare it twice and then compare it to find the minimum value, while 3*3/2-2 = 2.5

You can also use recursive analysis. Each increase in a number must be compared with the maximum and minimum values of the original array. The number of comparisons increases by 2. Therefore, the number of comparisons is 2n and a constant is added.