Last active
September 3, 2015 16:53
-
-
Save bluerogue/d9923e136c43eb42ef4f 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
/** | |
* 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