Topic:
Enter an array of integers to implement a function to adjust the order of the arrays in the array, so that all the odd digits are in the first half of the array, and the even digits are in the second half of the array.
Thinking: The array maintains two pointers, the first pointer initializes the array header, the second pointer initializes to the end of the array, the first pointer points to an even number, the second pointer to the number is always odd, and if the first pointer precedes the second pointer, the element that the pointer points to is swapped.
1 Packagesolution;2 3 /**4 * Sword refers to the offer surface question 14: Adjust array order is odd digit in front of even5 * Title: Enter an array of integers to implement a function to adjust the order of the numbers in the array so that all the odd digits are in the first half of the even number, and all the even digits are in the second half of the array6 * @authorGL7 *8 */9 Public classNo14reorderarray {Ten One Public Static voidMain (string[] args) { A int[] array={1,2,3,4,5,6,7,8,9}; - Reorderoddeven (array); - for(inti=0;i<array.length;i++){ theSystem.out.print (array[i]+ ","); - } - - } + - Public Static voidReorderoddeven (int[] Array) { + if(array==NULL|| Array.length<=0) A Throw NewRuntimeException ("Invalid array"); at intBegin=0; - intEnd=array.length-1; - while(begin<end) { - while(begin<end&& (array[begin]&1)!=0) -begin++; - while(begin<end&& (array[end]&1) ==0) inend--; - if(begin<end) { to inttemp=Array[end]; +array[end]=Array[begin]; -array[begin]=temp; the } * } $ }Panax Notoginseng -}
The Java implementation of the point of offer programming problem--face question 14 adjusting the array order so that the odd number is before even