Find Maximum algorithm (interview question)

Source: Internet
Author: User

 /*Find the maximum algorithm * 1. Assume that the first number is the maximum value * int max = arr[0]; * 2. Traverse the remaining elements, using the remaining elements to compare with Max, if the remaining elements are greater than Max, modify the max value to be larger * for (int i=1;i<arr.length;i++) {* if (Arr[i]>max) {* Max=arr[i]  ; *     }  * }  * */Importjava.util.Arrays; Public classMaxofarray { Public Static voidMain (string[] args) {int[] arr =New int[10];  for(inti=0;i<arr.length;i++) {Arr[i]=(int) (Math.random () *100);//assigning random numbers to arraysSystem.out.println (Arr[i]);//assignment good one output a}/*//for (int i=0;i<arr.length;i++) {System.out.println (arr[i]);//All values are assigned good after output}*/        intmax = arr[0];//assuming the first element is the largest         for(inti=1;i<arr.length;i++) {//traversing the remaining elements            if(Arr[i]>max) {//The remaining elements are compared to max, and if the remaining elements are larger than Max, modify the max value toMax=arr[i];//change the max value to a larger}}system.out.println ("Maximum value is:" +max); //Expand one capacity (the extended element is the default)arr = arrays.copyof (arr,arr.length+1); //assign Max Max to the last element in ArrARR[ARR.LENGTH-1] =Max;  for(inti=0;i<arr.length;i++) {System.out.println (arr[i]); }        }    }

Find Maximum algorithm (interview question)

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.