Sword refers to offer interview question 14 (Java edition): Adjust the array order so that the odd number is in front of the even number, the sword refers to offer

Source: Internet
Author: User

Sword refers to offer interview question 14 (Java edition): Adjust the array order so that the odd number is in front of the even number, the sword refers to offer

Question: enter an integer array and implement a function to adjust the order of numbers in the array so that all odd numbers are in the first half of the array, and all even numbers are in the second half of the array.

1. Basic implementation:

If you do not consider time complexity, the simplest idea is to scan the array from the ground up. Each time an even number is reached, this number is taken out, and move all the numbers behind the number to the front. After moving, there is a blank space at the end of the array. Then, the even number is put into this space. Since O (n) numbers need to be moved without an even number, the total time complexity is O (n2). However, this method cannot satisfy the interviewer. However, if we can tell this solution immediately after hearing the question, the interviewer will at least feel that our thinking is very agile.


2. A solution that only completes basic functions is only applicable to junior programmers.

This question requires that the odd number be placed in the first half of the array, and the even number be placed in the second half of the array. Therefore, all odd numbers should be located in front of the even number, that is, when we scan this array, if an even number is located before an odd number, we can swap their order, and the exchange will meet the requirements.

Therefore, we can maintain two pointers. When the first pointer is initialized, it points to the first number of the array, which only moves backward. When the second pointer is initialized, it points to the last number of the array, it refers to moving forward. Before the two pointers meet each other, the first pointer is always in front of the second pointer. If the number of the first pointer is an even number and the number pointed to by the second pointer is an odd number, we will exchange two numbers.


3. Considering the scalability solution, seckill Offer

If you are interviewing new graduates or programmers who have not been working for a long time, the interviewer will be satisfied with the previous Code. However, if you apply for a senior development position, the interviewer may ask several questions.

Interviewer: If you change the number of questions into an array and divide it into two parts by size, all negative numbers are before all non-negative numbers. What should you do?

If we change the question into dividing the number in the array into two parts, the number that can be divisible by 3 is before the number that cannot be divisible by 3. What should we do?

This is the interviewer's understanding of scalability, that is, we hope to provide a model in which we can expand existing solutions to the same type of problems.

Then we write the following code:

/*** Enter an integer array and implement a function to adjust the order of numbers in the array so that all odd numbers are in the first half of the array, all the even numbers are located in the second half of the array */package swordForOffer;/*** @ author JInShuangQi *** February */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] + ",");}}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.