快速排序
时间复杂度
最好情况:O(nlog2n)
最坏情况:O(n2)
平均情况:O(nlog2n)
稳定性
稳定性:不稳定
问题: 给你n个无序的像m的整型数,请使用快速排序实现从小到大的顺序。(0 <= n <= 5 * 104,-1 * 105 <= m <= 1 * 105)
public class QuickSort {
public static void sort(int[] a) {
quickSort(a, 0, a.length - 1);
}
private static void quickSort(int[] a, int low, int high) {
if (low < high) {
int pos = adjust(a, low, high);
quickSort(a, low, pos - 1);
quickSort(a, pos + 1, high);
}
}
private static int adjust(int[] a, int low, int high) {
int x = a[low];
while (low < high) {
while (low < high && a[high] > x) {
high--;
}
a[low] = a[high];
while (low < high && a[low] <= x) {
low++;
}
a[high] = a[low];
}
a[low] = x;
return low;
}
}