Partition an integers array into odd number first and even number second.
Do it in-place.
public void partitionArray(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
int left = 0;
int right = nums.length - 1;
while (left <= right) {
while (left <= right && !isEven(nums[left])) {
left++;
}
while (left <= right && isEven(nums[right])) {
right--;
}
if (left <= right) {// check again
int tmp = nums[left];
nums[left] = nums[right];
nums[right] = tmp;
left++;
right--;
}
}
}
private boolean isEven(int n) {
if (n % 2 == 0) {
return true;
} else {
return false;
}
}