Beauty of programming 2.10 finding the largest minimum in an array

Source: Internet
Author: User

Arrays are the simplest form of linear data structures, and when you get an array, you need to find the maximum minimum value, and what method can be used to find the maximum minimum value efficiently. For an array of n integers, how many times do you need to compare them?

There is now an array of n=8 {5,6,8,3,7,9,1,2}.

Solution One:

To find the maximum and minimum number as 2 independent problems, respectively solved, need to traverse the array 2 times, a total of 2N operations required.

1#include"iostream"2 using namespacestd;3 voidSearch_max_and_min (intBintN) {4     intmax,min;5max=min=a[0];6      for(intI=0; i<n;i++){7         if(a[i]>max)8max=A[i];9             Else if(a[i]<min)Tenmin=A[i]; One     } Acout<<"max="<<max<<Endl; -cout<<"min="<<min<<Endl; - } the intMain () { -     intData[] = {Ten,8,9,7,4,5}; -     intLength=sizeof(data)/sizeof(data[0]); - search_max_and_min (data,length); +}

Solution Two:

Usually the maximum and minimum number of cases will not be a number unless the n=1 or array is equal.

First, the adjacent 2 numbers are placed in the same group (the conceptual group), {(5,6) (8,3) (7,9)}, then compares adjacent numbers, places the smaller in the base digits, the larger is placed in the even digits, {(5,6) (3,8) (7,9)}, compares N/2 times, The resulting new array, we compare all the base digit elements and all the even bit elements, Max may only be on the even digits, Min may only be on the base bit. Then compare {5,3,7,1} and {6,8,9,2} respectively N/2 times, max=9,min=1, the whole algorithm compares 1.5N times;

Note : This method must satisfy the condition that n is an even number, and N is a cardinality when the correct answer may not be reached such as n=5,{3,4,3,4,5}, cardinality {3,3,5}, and even {4,4},max=4,min=3. Solution two or three has no technical situation, in the actual process is to be considered.

Solution Three:

The solution two destroys the original array, and we use the new method, without destroying the original array, to traverse, still put the adjacent 2 numbers in the same group (the conceptual group), {(5,6) (8,3) (7,9)}, and then use the variable max and min to store the current maximum minimum number, First compares the first second number, the large number in Max, the decimal in Min, then the second group (8,3) to know a[3]=8 large, a[4]=3 small, compare the decimal with min, less than the current min is updated, Max the same.

1#include"iostream"2 using namespacestd;3 voidSearch_max_and_min (intBintN) {4     intmax,min,tmax,tmin;5     6     if(N%2==0){7Max= (a[0]>a[1])? a[0]:a[1];8Min= (a[0]<a[1])? a[0]:a[1];9     }Ten     Else  Onemax=min=a[0]; A      for(intI=1; i<n/2; i++){ -         if(a[2*i-1]>a[2*i]) { -tmax=a[2*i-1]; thetmin=a[2*i]; -         } -         Else{ -tmax=a[2*i]; +tmin=a[2*i-1]; -         } +         if(Tmax >max) AMax =Tmax; at         if(Tmin <min) -Min =tmin; -          -     } -cout<<"max="<<max<<Endl; -cout<<"min="<<min<<Endl; in } - intMain () { to     intData[] = {Ten,8,9,7,4,5}; +     intLength=sizeof(data)/sizeof(data[0]); - search_max_and_min (data,length); the}

The comparison is still 1.5N, equivalent to a[2k] and a[2k+1] First a comparison, determined the size, and then respectively and max,min comparison, saving 0.5N times compared.

Solution Four:

Using the idea of divide and conquer, we find Max and min in n number, and then we find the number of Max and Min before and after N/2, then take the smaller min and the larger max. The number of comparisons is also 1.5N.

Beauty of programming 2.10 finding the largest minimum 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.