Last active
December 2, 2018 01:52
-
-
Save nanjingdaqi/53d1008b060eded7f755cf376fff70cc 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 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