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)