2nd-The charm of numbers-looking for the maximum and minimum values in an array

Source: Internet
Author: User

Find the maximum and minimum problem descriptions in the array

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 of "solution one"

You can think of the maximum and minimum values in the search array as two independent problems, and we will solve the problem by finding the maximum and minimum values for each group. The most straightforward approach is to first scan the array to find the maximum number and the minimum number. In this way, we need to compare the N-1 times to find the maximum number and the smallest number. The code is as follows:

1  PackageChapter2shuzizhimei.findminmax;2 /**3 * Find the maximum and minimum values in the array4 * "Solution One"5  * @authorDELL6  *7  */8  Public classFindminandmax {9     //look for the maximum and minimum values in array aTen      Public Static voidFindminmax (int[] a) { One         intMin = a[0]; A         intmax = A[0]; -          for(inti=0;i<a.length;i++){ -             if(a[i]<min) theMin =A[i]; -             Else if(a[i]>max) -Max =A[i]; -         } +SYSTEM.OUT.PRINTLN ("Minimum value is:" +min); -SYSTEM.OUT.PRINTLN ("Maximum value is:" +max); +     } A      Public Static voidMain (string[] args) { at         intA[] = {5, 6, 8, 3, 7, 9}; - Findminmax (a); -     } -  -}

The results of the program run as follows:

The minimum value is: 3 The maximum value is:9
"Solution Two"

The code is as follows:

1  PackageChapter2shuzizhimei.findminmax;2 /**3 * Find the maximum and minimum values in the array4 * "Solution two"5  * @authorDELL6  *7  */8  Public classFINDMINANDMAX2 {9     //look for the maximum and minimum values in array aTen      Public Static voidFindminmax (int[] a) { One         intTemp//realizing the adjacent odd-bit ratio is large A          for(intI=0;i<a.length;i=i+2){ -             if(a[i]<a[i+1]){ -temp =A[i]; theA[i] = a[i+1]; -A[I+1] =temp; -             } -         } +         intMin = a[1]; -         intmax = A[0]; +          for(intI=0;i<a.length;i=i+2){ A             if(a[i]>max) atMax =A[i]; -             if(a[i+1]<min) -Min = a[i+1];  -         } -SYSTEM.OUT.PRINTLN ("Minimum value is:" +min); -SYSTEM.OUT.PRINTLN ("Maximum value is:" +max); in     } -      Public Static voidMain (string[] args) { to         intA[] = {5, 6, 8, 3, 7, 9}; + Findminmax (a); -     } the  *}

The results of the program run as follows:

The minimum value is: 3 The maximum value is:9
"Solution three"

The code is as follows:

1  PackageChapter2shuzizhimei.findminmax;2 /**3 * Find the maximum and minimum values in the array4 * "Solution three"5  * @authorDELL6  *7  */8  Public classFindMinAndMax3 {9     //look for the maximum and minimum values in array aTen      Public Static voidFindminmax (int[] a) { One         intMin = a[0]; A         intmax = A[0]; -          for(intI=0;i<a.length;i=i+2){ -             if(a[i]>=a[i+1]){ the                 if(a[i]>max) -Max =A[i]; -                 if(a[i+1]<min) -Min = a[i+1];  +}Else{ -                 if(a[i]<min) +Min =A[i]; A                 if(a[i+1]>max) atmax = A[i+1];  -             } -      -         } -SYSTEM.OUT.PRINTLN ("Minimum value is:" +min); -SYSTEM.OUT.PRINTLN ("Maximum value is:" +max); in     } -      Public Static voidMain (string[] args) { to         intA[] = {5, 6, 8, 3, 7, 9}; + Findminmax (a); -     } the  *}

The results of the program run as follows:

The minimum value is: 3 The maximum value is:9
The thought of division and treatment of "solution four"

The specific code is as follows:

1  PackageChapter2shuzizhimei.findminmax;2 /**3 * Find the maximum and minimum values in the array4 * "solution four" divide and conquer thought5  * @authorDELL6  *7  */8  Public classFindMinAndMax4 {9     //defining class Two tuplesTen      Public Static classtuple{ One          Public intmin; A          Public intMax; -          PublicTuple (intMinintmax) { -              This. Min =min; the              This. Max =Max; -         } -     } -     //look for the maximum and minimum values in array a +      Public StaticTuple Findminmax (int[] A,intFirstintLast ) { -         if(last-first<=1){ +             if(a[first]<A[last]) A                 return NewTuple (A[first],a[last]); at             Else -                 return NewTuple (A[last],a[first]); -         } -Tuple ltuple = Findminmax (a,first,first+ (Last-first)/2); -Tuple rtuple = Findminmax (a,first+ (Last-first)/2+1, last); -         intMin,max; in         if(ltuple.min<rtuple.min) -Min =ltuple.min; to         Else +Min =rtuple.min; -         if(ltuple.max>Rtuple.max) theMax =Ltuple.max; *         Else $Max =Rtuple.max;Panax Notoginseng         return NewTuple (Min,max); -     } the      Public Static voidMain (string[] args) { +         intA[] = {5, 6, 8, 3, 7, 9}; ATuple t = Findminmax (a,0,a.length-1); theSYSTEM.OUT.PRINTLN ("Minimum value is:" +t.min); +SYSTEM.OUT.PRINTLN ("Maximum value is:" +T.max); -     } $  $}

The results of the program run as follows:

The minimum value is: 3 The maximum value is:9

2nd-The charm of numbers-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.