快速排序是由英国计算机科学家托尼·霍尔(Tony Hoare)在1960年提出的一种排序算法。它的基本思想是分治法(Divide and Conquer)的应用。
快速排序是一种高效的排序算法,它采用分治法的策略,将一个大的数组分割成两个小的子数组,并使左边子数组的所有元素都小于右边子数组的元素,然后递归地对这两个子数组进行快速排序。
快速排序的“快速”一词指的是它的平均时间复杂度较低,为O(n log n),其中n是待排序数组的长度。然而,在最坏情况下,其时间复杂度会退化为O(n^2)。
快速排序适用于大多数排序场景,特别是当数据量较大时。然而,由于它的不稳定性,对于需要保持相等元素顺序的场合,可能需要考虑其他排序算法。
假设有一个数组[4, 2, 8, 9, 5, 7, 1, 3, 6]
,我们选取第一个元素4
作为基准(pivot)。
[2, 1, 3, 4, 9, 7, 6, 8, 5]
,其中基准4
现在在中间位置。[2, 1, 3]
进行快速排序。[9, 7, 6, 8, 5]
进行快速排序。[1, 2, 3, 4, 5, 6, 7, 8, 9]
。public class QuickSort {
public static void quickSort(int[] array, int left, int right) {
if (left < right) {
int pivotIndex = partition(array, left, right);
quickSort(array, left, pivotIndex - 1);
quickSort(array, pivotIndex + 1, right);
}
}
private static int partition(int[] array, int left, int right) {
int pivot = array[left]; // 选择最左边的元素作为基准
int i = left + 1; // 从左边第二个元素开始扫描
for (int j = i; j <= right; j++) {
if (array[j] < pivot) {
// 交换array[i]和array[j]
swap(array, i, j);
i++;
}
}
// 将基准元素放到正确的位置
swap(array, left, i - 1);
return i - 1; // 返回基准元素的新位置
}
private static void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String[] args) {
int[] array = {4, 2, 8, 9, 5, 7, 1, 3, 6};
quickSort(array, 0, array.length - 1);
System.out.println(Arrays.toString(array)); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有