Skip to content

Instantly share code, notes, and snippets.

@lxxself
Last active August 16, 2018 01:32
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 lxxself/0c978b0f31289481963ab6d4a4beb074 to your computer and use it in GitHub Desktop.
Save lxxself/0c978b0f31289481963ab6d4a4beb074 to your computer and use it in GitHub Desktop.
public class QuickSort{
private void quickSort(int[] array, int startIndex, int endIndex) {
if (startIndex >= endIndex) {
return;
}
int p = part(array, startIndex, endIndex);
quickSort(array, startIndex, p-1);
quickSort(array, p + 1, endIndex);
}
private int part(int[] array, int startIndex, int endIndex) {
int piv = array[startIndex];
int left = startIndex;
int right = endIndex;
while (left != right) {
while (left < right && array[right] > piv) {
right--;
}
while (left < right && array[left] <= piv) {
left++;
}
if (left < right) {
int p = array[left];
array[left] = array[right];
array[right] = p;
}
}
array[startIndex] = array[left];
array[left] = piv;
return left;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment