max2--solving two large element values in an array

Source: Internet
Author: User

Method 1:

   Public classtest0920{ Public Static voidSwapint[] arr,intIintj) {          inttemp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; }       Public Static voidFindmax2values (int[] arr,intLowintHighintX1,intX2) {//find the maximum and 2nd-largest element values in the array in the interval [Low,high]           for(intXx:arr)                        {System.out.println (XX); }          if(Arr[x1=low]<arr[x2=low+1])//X1=low is called an assignment expression, and the value of an assignment expression is the value of the leftmost variable. swap (ARR,X1,X2);  for(intI=low+2;ii) {if(arr[i]>arr[x2])if(Arr[x1]<arr[x2=i])//very ingenious, in conjunction with the SWAP functionswap (ARR,X1,X2); } System.out.println ("Max2 Values:");        System.out.println (arr[x1]);              System.out.println (arr[x2]); }        Public Static voidMain (string[] args) {int[] Aa=New int[]{98,75,34,54,3900,38,18,20,9,23}; intX1=0; intX2=0; Findmax2values (AA,0, AA.LENGTH,X1,X2); }  }

max2--solving two large element 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.