Skip to content

Instantly share code, notes, and snippets.

@alisha
Created October 7, 2015 22:34
Show Gist options
  • Save alisha/b5705f36a91f33d6ae4c to your computer and use it in GitHub Desktop.
Save alisha/b5705f36a91f33d6ae4c to your computer and use it in GitHub Desktop.
private static int partition(int[] arr, int first, int last) {
int pivot = arr[(first + last)/2];
int i = first - 1; // index going left to right
int j = last + 1; // index going right to left
while (true) {
do {
i++;
} while (arr[i] < pivot);
do {
j--;
} while (arr[j] > pivot);
if (i < j)
Sort.swap(arr, i, j);
else
return j; // index of last element in the left subarray
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment