快速排序
public class QuickSort {
public static void main(String[] args) {
int[] arr = {0, 9, 6, 11, 3, 5, 12, 8, 10, 15, 14, 4, 1, 13, 7, 0};
int arrLength = arr.length - 1;
System.out.println("初始值:" + Arrays.toString(arr));
sort(arr, 0, arrLength);
System.out.println("排后值:" + Arrays.toString(arr));
}
public static void sort(int[] arr, int leftBound, int rightBound) {
if (leftBound >= rightBound) return;
int oldPivotIndex = partition(arr, leftBound, rightBound);
sort(arr, leftBound, oldPivotIndex - 1);
sort(arr, oldPivotIndex + 1, rightBound);
}
public static int partition(int[] arr, int leftBound, int rightBound) {
int pivot = arr[rightBound];
int left = leftBound;
int right = rightBound - 1;
while (left <= right) {
while (left <= right && arr[left] <= pivot) left++;
while (left <= right && arr[right] > pivot) right--;
if (left < right) swap(arr, left, right);
}
swap(arr, left, rightBound);
return left;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void partitionTwo(){
}
}