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

Source: Internet
Author: User

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

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.