-
Notifications
You must be signed in to change notification settings - Fork 1
/
JAVA_21.java
47 lines (41 loc) · 1.08 KB
/
JAVA_21.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package chapter3;
import java.util.Arrays;
/**
* 调整数组顺序使奇数位于偶数前面
* <p>
* [2,3,4,5,6,6] => [3,5,2,4,6,6]
* <p>
* 考察点:左右指针
*/
public class JAVA_21 {
public static void main(String[] argv) {
int[] array1 = {1, 2, 3, 4, 5, 6, 7, 8, 9};
resort(array1);
System.out.println(Arrays.toString(array1));
}
public static void resort(int[] array) {
if (array.length <= 1) {
return;
}
int left = 0, right = array.length - 1;
while (left < right) {
while (left < right && isOdd(array[left])) {
left++;
}
while (right > left && !isOdd(array[right])) {
right--;
}
if (left < right) {
swap(array, left, right);
}
}
}
private static boolean isOdd(int value) {
return (value & 0x1) == 1;
}
private static void swap(int[] array, int a, int b) {
int tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
}