# Programmer interview question selection (29): Adjust the array order so that the odd number is in front of the even number

Source: Internet
Author: User

Question: enter an integer array 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. The time complexity is O (n ).

Analysis: If time complexity is not taken into account, the simplest idea should be 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 one by one. After moving, there is a blank space at the end of the array. Then, the even number is put into this space. Since an even number needs to be moved to an O (n) number, the total time complexity is O (n2 ).

It is required 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 to say, when we scan this array, if an even number exists before the odd number, we can swap their order, and after the exchange, it will meet the requirements.

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

Based on this idea, we can write the following code:

Void reorder (int * pdata, unsigned int length, bool (* func) (INT ));
Bool iseven (int n );

//////////////////////////////////////// /////////////////////////////////
// Devide an array of integers into two parts, odd in the first part,
// And even in the second part
// Input: pdata-an array of Integers
// Length-the length of Array
//////////////////////////////////////// /////////////////////////////////
Void reorderoddeven (int * pdata, unsigned int length)
{
If (pdata = NULL | length = 0)
Return;

Reorder (pdata, length, iseven );
}

//////////////////////////////////////// /////////////////////////////////
// Devide an array of integers into two parts, the intergers which
// Satisfy func In the first part, otherwise in the second part
// Input: pdata-an array of Integers
// Length-the length of Array
// Func-a function
//////////////////////////////////////// /////////////////////////////////
Void reorder (int * pdata, unsigned int length, bool (* func) (INT ))
{
If (pdata = NULL | length = 0)
Return;

Int * pbegin = pdata;
Int * pend = pdata + Length-1;

While (pbegin <pend)
{
// If * pbegin does not satisfy func, move forward
If (! Func (* pbegin ))
{
Pbegin ++;
Continue;
}

// If * pend does not satisfy func, move backward
If (func (* pend ))
{
Pend --;
Continue;
}

// If * pbegin satisfy func while * pend does not,
// Swap these Integers
Int temp = * pbegin;
* Pbegin = * pend;
* Pend = temp;
}
}

//////////////////////////////////////// /////////////////////////////////
// Determine whether an integer is even or not
// Input: an integer
// Otherwise return false
//////////////////////////////////////// /////////////////////////////////
Bool iseven (int n)
{
Return (N & 1) = 0;
}

Discussion:

The above code has three points worth mentioning:

1. The iseven function is used to determine whether a number is an even number. Instead, it uses the "&" operator &. Generally, bitwise operators are faster than %;

2. This question has many variants. The requirement here is to put the odd number in front of the even number. If the requirement is changed to: Put the negative number in front of the non-negative number, the idea is the same.

3. In the reorder function, use the function pointer func to point to determine whether a number meets the given conditions, rather than using the code to directly judge (hard code ). The advantage of this is that the algorithm for adjusting the order is separated from the adjustment standard (I .e. decoupling, decouple ). When the adjusted standard changes, the reorder Code does not need to be modified. You only need to provide a new function to determine and adjust the standard, which improves the maintainability of the Code. For example, we need to place the negative number before the non-negative number. We don't need to modify the reorder code. We only need to add a function to determine whether the integer is not a negative number. This idea is widely used in many libraries. For example, many Algorithm functions in STL have a parameter of the function (functor). (Of course, the function is not a function pointer, but the idea is the same ). If you can think of this layer in the interview, you will undoubtedly be able to leave a good impression on the interviewer.

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.