# "Sword refers to offer study" "Face question 14: Adjust the array order so that the odd number is in front of even"

Source: Internet
Author: User

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 array, and all the even digits are given to the second half of the array.

This topic requires that the odd number be placed in the first half of the array, even in the second half of the array, so all the odd numbers should be in front of even numbers. That is, when we scan this array, if we find an even number in front of the odd number, we can exchange them in order, and then meet the requirements after the exchange.

So we can maintain two pointers, and the first pointer initializes to the first number of the array, it moves backwards: The second pointer is initialized to the last digit of the array, and it moves forward only. Before the two pointers meet, the first pointer is always in front of the second pointer. If the number that the first pointer points to is even, and the second pointer points to an odd number, we swap the two numbers.

Code implementation:

`` Public  class Test14 {    /** * Enter an array of integers to implement a function to adjust the order of the numbers in the array, so that all odd digits are in the first half of the array, and all even digits are given to the second half of the array. * * @param An array of arr input */     Public Static void Reorderoddeven(int[] arr) {//For the input array is empty, or the length of less than 2 is only returned        if(arr = =NULL|| Arr.length <2) {return; }//Record even position from left to right        intStart =0;//record odd position from right to left        intEnd = Arr.length-1;//Start adjusting odd and even positions         while(Start < end) {//Find even             while(Start < end && Arr[start]%2!=0) {start++; }//Find odd numbers             while(Start < end && Arr[end]%2==0) {end--; }//Find to swap the odd and even positions            //For the start=end situation, the exchange will not have any impact            //So the If judgment is omitted            intTMP = Arr[start];            Arr[start] = Arr[end];        Arr[end] = tmp; }    }/** * Output Array information * * @param arr to output array */     Public Static void PrintArray(int[] arr) {if(Arr! =NULL&& arr.length >0) { for(intI:arr) {System.out.print (i +" ");        } System.out.println (); }    } Public Static void Main(string[] args) {int[] Array = {0,1,2,3,4,5,6,7,8,9};        Reorderoddeven (array);    PrintArray (array); }}``

Operation Result:

"Sword refers to offer study" "Face question 14: Adjust the array order so that the odd number is in front of even"

Related Keywords:

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.