Adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

The title description enters an array of integers, implements a function to adjust the order of the numbers in the array, so that all the odd digits are placed in the first half of the array, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even are guaranteed.
 Public classSolution { Public voidReorderarray (int[] Array) {        intJishu=0; intOushu=0; intI=0; intK=0;  while(i<array.length&&k<array.length) {             while(i<array.length) {                if(array[i]%2==0) {Oushu=Array[i];  Break; } I++; } k=i+1;  while(k<array.length) {                if(array[k]%2!=0) {Jishu=Array[k];  Break; } k++; }            if(k<array.length) {                 for(intj=k;j>i;j--) {Array[j]=array[j-1]; } array[i++]=Jishu; }Else{                 Break; }                    }    }}

This problem is not difficult, the beginning of the thinking wrong, half a day not around the bend ...

1. Take into account, if the technology and even the search from the back then, each time to find even after the odd number from the current position, more trouble, why not find even from the front, from behind to find odd ... Later proved himself too foolish *, did not see clearly the problem, require odd even relative position unchanged

2. Then find an even number from the beginning, find the odd number from the location of the even number, find and then swap them ... It's silly. Doing so will change the relative position of the odd even numbers in their respective positions.

3. Positive solution: Each time you find the previous even and after the odd number, then the between the part (including that even number) is unified and then move one bit, because the between is an even number (from the found even after the first odd number found and start to find the number of even between the number must be), There were no even numbers before (because looking for the first even number from left to right), they were unified and then moved one, so that the odd number that was saved in advance was placed in front of the empty one. So the equivalent of 132457 to 5 to 2 4 of the two even-numbered front, 1 and 5 would have been sequential. In this way, the relative order of the odd and even after the adjustment of the fort will not change.

4,. The last small point needs to be noted, that is, in the final unification after moving an operation, you need to determine whether the range k is in accordance with the specification, because there may be no break in front, causing K to become the maximum value of array.length.

The worst time complexity of this solution is O (n squared); Because finding even complexity is N, finding the odd number in the back plus moving the numbers is also the n level of complexity, the square of the total n

The use of space for time, new an array, from the beginning of the traversal, encountered even saved into the new array, and delete the original even, and finally the even part of the portion of the odd part after the division.

This can use a dynamic array, you can also find the number of odd number of N, the time of traversal from 1 to save a few, starting from N to save even. Time Complexity of O (n)

Adjust the array order so that the odd digits are preceded by even numbers

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.