Java实现快速排序
import java.util.Arrays;
public class SortTest{
public static void QuickSort(int[] arr, int lower, int upper) {
if (lower > upper) {
return;
}
int pivot = arr[lower];
int i = lower, j = upper;
while (i<j) {
while (pivot <= arr[j] && i<j) {
j--;
}
while (pivot >= arr[i] && i<j) {
i++;
}
if (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
arr[lower] = arr[i];
arr[i] = pivot;
QuickSort(arr, lower, i-1);
QuickSort(arr, i+1, upper);
}
public static void main(String[] args) {
int[] arr = new int[]{23, 56, 1, -56, 4, 8, 98, 56, 0, 100, 101, 102};
System.out.println(Arrays.toString(arr));
QuickSort(arr, 0, arr.length-1);
System.out.println(Arrays.toString(arr));
}
}