-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava_2_7_1.java
More file actions
47 lines (41 loc) · 1.3 KB
/
java_2_7_1.java
File metadata and controls
47 lines (41 loc) · 1.3 KB
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 chapter2;
import java.util.Arrays;
/**
* 荷兰国旗
* <p>
* 有n个红白蓝三种颜色的小球,乱序排列,请通过两两交换,使得顺序为红白蓝
*/
public class java_2_7_1 {
public static void main(String[] argv) {
int[] nums = {2, 2, 0, 1, 2, 1, 2, 0, 2, 1, 0};
colorSort(nums);
System.out.println(Arrays.toString(nums));
}
/**
* 把红白蓝三种颜色分别对应成0,1,2
* 利用三个指针排序,前指针负责把中指针的0交换到数组前面,后指针负责把中指针的2交换到数组后面
*/
public static void colorSort(int[] nums) {
if (nums == null || nums.length == 0) return;
int left = 0, right = nums.length - 1, center = left;
while (center < right) {
if (nums[center] == 0) {
swap(nums, left, center);
center++;
left++;
} else if (nums[center] == 1) {
center++;
} else if (nums[center] == 2) {
swap(nums, center, right);
right--;
}
}
}
private static void swap(int[] nums, int i, int j) {
if (i != j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}