Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created March 21, 2014 22:26
Show Gist options
  • Select an option

  • Save rayjcwu/9697757 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9697757 to your computer and use it in GitHub Desktop.
public void evenOdd(int[] A) {
// [0: even - 1] // even
// [even: odd] // unsorted
// [odd + 1: A.length - 1] // odd
int even = 0;
int odd = A.length - 1;
while (even <= odd) {
if (A[even] % 2 == 0) {
even++;
} else {
swap(A, even, odd);
odd--;
}
}
}
private void swap(int[] A, int i, int j) {
int t = A[i];
A[i] = A[j];
A[j] = t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment