[Bovine prove safety off the offer] to adjust the order of the array so that in front of the even-odd

Link : https: //www.nowcoder.com/practice/beb5aa231adc45b2a5dcc5b62c93f593

Title : Enter an array of integers, to realize a function to adjust the order of the numbers in the array, such that all the odd part of the front half of the array, all even located in the second half of the array, and to ensure that between odd and odd-even and even- the relative positions unchanged.

 

Ideas : time for space, opened with an array of even-preservation, scan the original array, the even number stored in the temporary array and the odd move to the left, and finally even the temporary array back on the tail of the original array.

 

For example

Source array {3,4,5,7,8,2,9}

The beginning is this:

Scan from left to right, the first number is three, the odd, to put 3 into the insertion pointer (actually in place), and then the insertion pointer moves to the right, the scanning pointer is also moved to the right.

then:

 

Now Scan to 4, is an even number, put it into a temporary array, and the scanning pointer forward.

 then:

 

5 is now scanned, is an odd number, place the insertion pointer at 5, and then move the insertion pointer to the right, the scanning pointer is also moved to the right.

then:

Scan to 7, is an odd number, place the insertion pointer at 7, and then move the insertion pointer to the right, the scanning pointer is also moved to the right.

then:

Now scanned 8, is an even number, put it into a temporary array, and the scanning pointer forward.

then:

And then:

At last:

So far, the left has completed the odd and even elected.

Next to the even temporary array into the original array can be.

 

Code

 1 /**
 2  * @author yuan
 3  * @version 0.1
 4  * @date 2019/6/6
 5  */
 6 public class ReOrderArray {
 7 
 8     public static void print(int[] array) {
 9         for (int i = 0; i < array.length; i++) {
10             System.out.print(array[i]);
11             if (i == array.length - 1) {
12                 System.out.println();
13             } else {
14                 System.out.print(" ");
15             }
16         }
17     }
18 
19     public static void reOrderArray(int[] array) {
20         // 保存偶数
21         int[] temp = new int[array.length];
22         // 当前偶数数量
23         int count = 0;
24         // 当前填入位置,第一轮for循环用于奇数左移,第二轮用于填入偶数
25         int pos = 0;
26         for (int i = 0; i < array.length; i++) {
27             if ((array[i] & 1) == 0) {
28                 temp[count++] = array[i];
29             } else {
30                 array[pos++] = array[i];
31             }
32         }
33         for (int i = 0; i < count; i++) {
34             array[pos++] = temp[i];
35         }
36     }
37 
38     public static void main(String[] args) {
39         int[] array = new int[]{3, 4, 5, 7, 8, 2, 9};
40         print(array);
41         reOrderArray(array);
42         print(array);
43     }
44 }

 

Guess you like

Origin www.cnblogs.com/null-0/p/10988709.html