Skip to content

Instantly share code, notes, and snippets.

@bluerogue
Last active September 3, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bluerogue/d9923e136c43eb42ef4f to your computer and use it in GitHub Desktop.
Save bluerogue/d9923e136c43eb42ef4f to your computer and use it in GitHub Desktop.
/**
* Recursive quicksort method. Pivot is calculated at 1/2 the difference
* between supplied bounds.
*
* @param lowerBound
* the lower bound of indexed inputs - e.g., usually 0
* @param upperBound
* the upper bound of indexed inputs - e.g., usually array.length
* @param array
* the array to sort
*/
private static void quickSort(int lowerBound, int upperBound, int[] array) {
int i = lowerBound;
int j = upperBound;
int pivot = array[lowerBound + (upperBound - lowerBound) / 2];
while (i <= j) {
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
i++;
j--;
}
}
if (lowerBound < j) {
quickSort(lowerBound, j, array);
}
if (i < upperBound) {
quickSort(i, upperBound, array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment