Use two pointers, moving relative to the tail of the array;
Loop End Condition: Head and tail overlap alive head behind tail
Left pointer right shift condition: Current number is odd
Right pointer left shift condition: Current number is even
When and only if the left pointer is even, the right hand is odd, swapping the value of two pointers
This topic notes the extension, reuse of function functions.
PackageOffer ;Importjava.util.Arrays;/*face question 14: Adjust the array order so that the odd number is preceded by even the first question: Enter an array of integers, implement a function to adjust the order of the numbers in the function array, so that all the odd digits are in the first half of the array, and all the arrays are in the second half of the array. */ Public classProblem14 { Public Static voidMain (string[] args) {PROBLEM14 test=NewProblem14 (); int[] Array =New int[]{1,6,8,7,4,3,0,90,19}; intLength =Array.Length; Test. Sortarray (array, length); for(inti = 0;i<length;i++) {System.out.print (Array[i]+ " "); } } Public voidSortarray (intArray[],intlength) { intEnd = Length-1; intStart = 0; if(array==NULL|| Length==0){ return; } while(start<end) { while(!isevent (Array[start])) {Start++; } while(Isevent (Array[end])) {End--; } while(Start<end && isevent (Array[start]) &&!isevent (Array[end])) { inttemp =Array[end]; Array[end]=Array[start]; Array[start]=temp; Start++; End--; } } } //determines the current position value parity, even returns true, Odd returns false Public BooleanIsevent (intItem) { //return item && Ox01; returnItem%2==0; }}
Interview 14: Adjust the array order so that the odd digits are preceded by even numbers