Skip to content

Instantly share code, notes, and snippets.

@nanjingdaqi
Last active December 2, 2018 01:52
Show Gist options
  • Save nanjingdaqi/53d1008b060eded7f755cf376fff70cc to your computer and use it in GitHub Desktop.
Save nanjingdaqi/53d1008b060eded7f755cf376fff70cc to your computer and use it in GitHub Desktop.
public void quickSort(int[] arr) {
if (arr == null || arr.length <= 1) return;
quickSort(arr, 0, arr.length);
}
public void quickSort(int[] arr, int s, int e) {
if (arr.length == 0 || s >= e) return;
int sepIndex = partition(arr, s, e);
quickSort(arr, s, sepIndex);
quickSort(arr, sepIndex + 1, e);
}
public int partition(int[] arr, int s, int e) {
if (s == e) {
return s;
}
int pivot = arr[e-1];
int i = s;
int j = s;
while (i < e-1) {
int val = arr[i];
if (val < pivot) {
swap(arr, i, j);
j++;
}
i++;
}
swap(arr, e-1, j);
return j;
}
public void swap(int[] arr, int i, int j){
int tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment