[Interview Questions] use the least number of comparisons to find the maximum and secondary values in an array.

Source: Internet
Author: User
Description

Such as the question and no repeated Elements

The idea is to use the divide and conquer method to divide the array into two groups. The two groups respectively use the two comparison elimination methods to find their respective maximum values (a total of N-1 comparisons). The final two maximum values are compared, the larger one is the maximum value, and the second largest value is generated between the smaller one and all elements that have been compared with the maximum value (a total of log_2 N-1. You only need to use the smaller one.
And log_2 N-1 compared with the maximum value respectively (here we need to compare log_2 n-1 times) on the line. So the final number of comparisons is (logn + N-2 ).

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.