Created
October 7, 2015 22:34
-
-
Save alisha/b5705f36a91f33d6ae4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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