快速排序
原理:基于“二分”思想的递归
public static void main(String[] args) {
int[] arr = {10, 7, 5, 4, 7, 62, 3, 6, 2, 1, 8, 9, 19};
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
private static void quickSort(int arr[], int low, int hight) {
//没有判定:Exception in thread "main" java.lang.StackOverflowError
if(low>hight){
return;
}
int temp = arr[low];
int i = low, j = hight;
while (i < j) {
while (i < j && temp <= arr[j]) {//TIPS:小于等于
j--;
}
while (i < j && temp >= arr[i]) {//TIPS:小于等于
i++;
}
if(i<j){
int t = arr[j];
arr[j]= arr[i];
arr[i] =t;
}
}
arr[low]=arr[i];
arr[i]=temp;
quickSort(arr,low,j-1);
quickSort(arr,j+1,hight);
}