Last active
August 16, 2018 01:32
-
-
Save lxxself/0c978b0f31289481963ab6d4a4beb074 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
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