时间复杂度:平均O(nlogn) 最好O(nlogn) 最坏O(n²)
空间复杂度:O(logn)
稳定性:不稳定
特点:n大时较好
public class QuickSort {
public static void main(String[] args) {
int[] a = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 };
quickSort(a, 0, a.length - 1);
for (int n : a) {
System.out.print(n + " ");
}
}
public static void quickSort(int[] a, int low, int high) {
if (a == null) {
return;
}
if (low >= high) {
return;
}
int i, j, n;
i = low;
j = high;
n = a[i];
while (i < j) {
while (i < j && a[j] >= n) {
j--;
}
if (i < j) {
a[i++] = a[j];
}
while (i < j && a[i] < n) {
i++;
}
if (i < j) {
a[j--] = a[i];
}
}
a[i] = n;
quickSort(a, low, i - 1);
quickSort(a, i + 1, high);
}
}