Finding the minimum value of the maximum value in an array

Source: Internet
Author: User

Solution One:

The maximum and minimum values in the array are evaluated separately. Scan through the array first to find the maximum number and the smallest number. Need to compare 2*n times.


Solution Two:

In general, the maximum and minimum values are not the same. So divide the array into two parts, and then find the maximum and minimum values from the two parts respectively.


Finally, the max=9,min=3 are obtained from the odd and even digits, each need to compare N/2 times, the whole algorithm needs to compare 1.5*n times.


Solution Three:

The solution two destroys the array, and if it is compared during the traversal without exchanging elements, it is not possible to break the array. First, the contiguous two numbers in the array are still sorted in the same group (conceptually grouped), and then two variables Max, and Min are used to store the maximum and minimum values, then the two numbers of the same group are compared, but not the order, but the min and the smaller values are compared. if it is smaller than min, then update min is the value. Max and the larger value comparison,

Finally, max=9,min=3. However, the complexity of time is not reduced, and the number of comparisons in the process is still 1.5*n times.


Solution Four:
Using the idea of divide and conquer, in the N number to find the maximum max and Min min, only need to find out the number of Min and Max before and after N/2, and then take the smaller min, and the larger Max can be. (Only a larger number and a larger number comparison, a smaller number and a smaller number can be compared two times).

Suppose we ask for the maximum and minimum values of the ARR[1,2,3.....N] array, the pseudo-code is as follows:

(Max,min)Search(Arr,b, e) {if(b-e<=1){if(arr[b]<arr[e]) {return(arr[e],arr[b]);}Elsereturn(arr[b],arr[e]);(maxl,minl) =Search(Arr,b,b+ (E-b)/2);(Maxr,minr) =Search(Arr,b+ (E-b)/2+1, e);if(MAXL&GT;MAXR) Maxv=minl;ElseMaxv=minr;if(MINL&LT;MINR) Minv=minl;ElseMinv=minr;return(MAXV,MINV);}

Finding the minimum value of the maximum value 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.