Interview 14: Adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

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

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.