At the same time, we can find the optimal algorithms for the maximum and minimum numbers, and find the minimum number of comparisons required for the two largest numbers.

Source: Internet
Author: User

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.

At the same time, we can find the optimal algorithms for the maximum and minimum numbers, and find the minimum number of comparisons required for the two largest numbers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.