"Beauty of programming" looking for the maximum and minimum values in an array

Source: Internet
Author: User

Arrays are the simplest form of data structure. One of the basic problems we often encounter is finding the largest number in the entire array, or the smallest number. At this point, we will scan the array once and find out the maximum (minimum) number. What if we need to find both the maximum and the smallest number?

For an array of n integers, how many times do you need to compare the maximum and minimum numbers to find out?

Analysis and Solution

Solution One: To find the maximum and minimum values respectively

The maximum and minimum values of the array can be calculated separately so that we need to compare 2N times to solve.

Solution Two: Group solution

(1) In the order of the array adjacent to the two numbers in the same group;

(2) Compare the two numbers of the same group, put the large number on the even digits, and put the small on the odd digits;

(3) Finally, the maximum value is obtained from the even digits, and the minimum value can be obtained on the odd digits.

You need to compare 1.5N times altogether. This approach, though less frequently compared, destroys the original array.

Solution Three: Improved grouping

(1) The current maximum and minimum values are stored with two variables max and Min respectively.

(2) After the two numbers of the same group are compared, the order is no longer adjusted, and the larger ones are compared with the current Max, and the smaller ones are compared with the Min.

(3) Repeat until the complete array is traversed.

The overall process is compared to 1.5N times.

Solution Four: The strategy of division and treatment

The number of Min and Max before and after N/2 are calculated respectively, then the smaller min, the larger max can be obtained.

Need to compare the number of times for 1.5n-2, analysis slightly.

Scaling Issues

If you need to find the second largest number in N, how many times do you need to compare it? Is it possible to use similar divide-and-conquer ideas to reduce the number of comparisons?

Idea One:

Maximum Max and sub-large ans are stored with two variables, To iterate over an array:

If arr[i] > max, update Max=arr[i],ans=max;

Otherwise if arr[i] > ans, then update ans=arr[i];

Otherwise, the update operation is not performed.

The maximum number of comparisons for this method is 2*n times and the smallest n times.

Idea two: Fast selection algorithm

Looking for a special case of the K-large integer problem, the analysis of the number of comparisons seems somewhat difficult.

"Beauty of programming" looking for the maximum and minimum values in an array

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.