快速排序
public static void main(String[] args) {
int[] arr = {0, 5, 9, 1, 3, 6};
//int partition = partition(arr, 0, arr.length - 1);
quick(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
public static void quick(int[] arr, int l, int r) {
if (l < r) {
int mid = partition(arr, l, r);
quick(arr, l, mid-1);
quick(arr, mid+1 , r);
}
}
public static int partition(int arr[], int l, int r) {
int pivot=arr[r];
int cur=l;
for (; l < arr.length-1;l++) {
if (arr[l] < pivot) {
swap(arr, cur++, l);
}
}
swap(arr, cur, r);
return cur;
}
private static void swap(int[] arr, int i, int r) {
int temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
标签:arr,cur,示例,int,代码,partition,static,quick,排序
From: https://www.cnblogs.com/BitX/p/18311297