Algorithm title: Put all the odd numbers in an array in front and even behind (do not open new memory space)

Source: Internet
Author: User

 Packagearithmetic;Importjava.util.Arrays; Public classOddandeven { Public Static voidMain (string[] args) {int[] a={5,10,26,32,41,7,9,8,4,12,1};; intLow = 0; intHigh = A.length-1; /**idea: * 1. Traverse. Put the odd number on the left and the even on the right * 2. Walk to the left until the time is not an odd number * 3. Then traverse to the right until there is no even number * 4. Swap location*/          while(Low <High ) {                inti =Low ; //iterate from the left of the array                 while(a[i]%2! = 0) { low++; I=Low ; }                                intindex = i;//The current number of even-numbered positions subscripti =High ; //from the right of the array, start traversing                 while(a[i]%2 = = 0) { high--; I=High ; }                //Exchange                if(Low <High ) {                    intTMP =A[index]; A[index]=A[i]; A[i]=tmp;    }} System.out.println (Arrays.tostring (a)); }}

Algorithm title: Put all the odd numbers in an array in front and even behind (do not open new memory space)

Related Article

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.