Java finds the maximum value of a set of numbers

Source: Internet
Author: User

form: int [] nums = {7,2,8,9,1,12};

Jie Yi: 22 Compare and record the subscript, the next comparison with the last comparison of the maximum value and the last comparison of the next comparison, the cycle to find the maximum value

1 /**2      * @authorhorse-to-peak comparison find the maximum value again3      * @paramarr4      * @return5      */6     Private Static intGetmaxnum (int[] arr) {7 8         //Record the subscript9         intindex = 0;Ten         //Suppose the first one is the maximum value One         intmax = Arr[0]; A          for(inti = index; i < arr.length; i++) { -             if(i + 1 < arr.length && Max < Arr[i + 1]) { -index = i + 1; themax = arr[i + 1]; -             } -         } -         returnMax; +}
View Code

Solution Two: Bubble Sort method

1 Private Static intFindmaxnum (int[] arr) {2 3          for(inti = 0; i < arr.length; i++) {4              for(intj = 0; J < arr.length-i-1; J + +) {5                 if(Arr[j] < Arr[j + 1]) {6                     inttemp =Arr[j];7ARR[J] = arr[j + 1];8Arr[j + 1] =temp;9                 }Ten             } One         } A  -         returnArr[0]; -}
View Code

Java finds the maximum value of a set of numbers

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.