# Sword Point (Java Edition): Adjust the array order so that the odd digits precede the even number

Source: Internet
Author: User

Title: Enter an array of integers. Implement a function to adjust the order of the numbers in the array. Causes all odd digits to be in the first half of the array. All even digits are in the second half of the array.

1, the basic realization :

Assuming that the time complexity is not considered, the simplest idea should be to scan the array from the beginning, take out this number each time an even number is encountered, and move all the numbers that are behind the number forward.

After the move, there is an empty space at the end of the array. Then put the even number in the empty space.

You need to move o (n) numbers without touching an even number. So the total time complexity is O (N2). However, such a method does not make the interviewer comfortable. Just suppose that we can say this solution after we hear the subject, and the interviewer will at least think our thinking is very sensitive.

2, just complete the basic function of the solution, only applicable to 0 basic program Ape

This topic requires that the odd number be placed in the first half of the array. The even number is placed in the second half of the array. So all the odd numbers should be in front of an even number, that is, when we scan this array, suppose we find that even in the odd-numbered front, we can exchange their order, and then meet the requirements after the exchange.

So we are able to maintain two pointers, and the first pointer initializes with the first digit of the array, and it simply moves backwards. The second pointer is initialized to point to the last digit of the array. It means moving forward. Before the two pointers meet, the first pointer is always in front of the second pointer. Let's say the number of the first pointer is even, and the second pointer points to an odd number, we exchange two numbers.

3, consider the solution of extensibility. Second-kill offer

Suppose to be an interview with a recent graduate or a program ape that is not working long hours. The interviewer will be comfortable with the previous code. But suppose the candidate is applying for a senior development position. The interviewer may go on to ask a few questions.

Interviewer: Suppose to change the number of questions in the group according to the size of the two parts, all negative numbers in the front of all non-negative, how to do?

If we change the subject, we can divide the number of the array into two parts, and the number divisible by 3 is in front of the number that cannot be divisible by 3. What to do?

This is where the interviewer is examining our understanding of extensibility. It is hoped that we can give a pattern in which the existing solution can be extended to the same type of problem in a very important way.

So we write the following code:

`/** * 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 in the second half of the array */package swordforoffer;/** * @author Jinshuangqi * August 1, 2015 */public class E14reorderarray {public void order (int[] arr) {if (arr = = null) Return;int i = 0;int j = arr.length- 1;while (i<j) {if (IsEven (Arr[i]) &&!iseven (Arr[j])) {int temp = arr[i];arr[i]= Arr[j];arr[j] = temp;} else if (!iseven (Arr[i]) && IsEven (Arr[j])) {i++;} else if (IsEven (Arr[i]) && IsEven (Arr[j])) {j--;} else{i++;j--;}}} public boolean isEven (int n) {return (n & 1) = = 0;} public static void Main (string[] args) {E14reorderarray test = new E14reorderarray (); int[] arr= {1,2,3,4,5,6,12,7,8,9,10} ; Test.order (arr); for (int i = 0;i<arr.length; i++) {System.out.print (arr[i]+ ",");}}}`

Sword Point (Java Edition): Adjust the array order so that the odd digits precede the even number

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.