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 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.

1, the basic realization :

Without considering the complexity of the time, the simplest idea would be to scan the array from scratch, take out this number each time an even number is encountered, and move all the numbers that are behind that number to the front. After you have moved, you have an empty space at the end of the array, and then put the even number in the empty space. The total time complexity is O (N2), since it is necessary to move o (n) numbers without touching an even number. However, this method does not satisfy the interviewer. But if we can say this solution immediately after we hear the question, the interviewer will at least feel that our thinking is very sensitive.


2, only complete the basic function of the solution, only for the novice programmer

This topic requires the odd number to be placed in the first half of the array, even in the second half of the array, so that all the odd numbers should be in front of even numbers, that is, when we scan the array, if we find that there are even digits in front of the odd, we can exchange their order, and then meet the requirements.

So we can maintain two pointers, the first pointer to the first digit of the array, it moves backwards, and the second pointer initializes to the last digit of the array, which refers to moving forward. Before the two pointers meet, the first pointer is always in front of the second pointer. If the number of the first pointer is even, and the second pointer points to an odd number, we exchange two digits.


3, consider the solution of extensibility, second kill offer

If you are interviewing a recent graduate or a programmer who is not working long hours, the interviewer will be happy with the code above, but if the candidate is applying for a senior development position, the interviewer may ask a few questions.

Interviewer: If the number of questions in the group is divided into two parts according to the size, all negative numbers in front of all non-negative, what should be done?

If you change the title to change the number of the array into two parts, can be divisible by 3 of the number is not divisible by 3 in front of the number, how to do?

This is where the interviewer is looking at our understanding of extensibility, which we hope we can give a pattern in which the existing solution can be extended to the same type of problem.

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]+ ",");}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.