the left part of left pointer is less than 1. [.., left)
the right part of right pointer is greater than 1. (right, ...]
the part between left pointer and i is equal to 1. [left, i)
public class sortColors {
public static void sortColors(int[] nums) {
if(nums==null||nums.length==0)return;
int len = nums.length;
int left = 0, right = len-1, i = 0;
while (i <= right){
System.out.println(left+" "+i+" "+right);
System.out.println(Arrays.toString(nums));
if (nums[i] < 1){
swap(nums, left, i);
left++;
i++;
} else if (nums[i] == 1) {
i++;
} else {
swap(nums, i, right);
right--;
}
System.out.println(left+" "+i+" "+right);
}
return;
}
private static void swap(int[] nums,int i,int j){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
public static void main(String[] args) {
int [] nums = new int[] {2,0,0,1,1,2,2,0};
sortColors(nums);
}
}