浅谈快速排序

来源:互联网 发布:linux指令大全 编辑:程序博客网 时间:2024/05/03 16:28

快速排序:

指定基准记录numbers[low](通常为第一个元素),通过一趟排序将其放在正确的位置上,把待排序元素分割为独立的两部分,左边部分元素值<= numbers[low] <=右边部分元素值;

对左右两部分元素重复上述过程,依次类推,直到子序列中只剩下一个记录或不含记录为止。

public static void quickSort(int[] numbers,int low,int high) {if(low < high) {int mid = partition(numbers, low, high);quickSort(numbers, low, mid - 1);quickSort(numbers, mid + 1, high);}}public static int partition(int[] numbers,int low,int high) {int temp = numbers[low];while (low < high) {while (low < high && numbers[high] > temp)high--;numbers[low] = numbers[high];while (low < high && numbers[low] < temp)low++;numbers[high] = numbers[low];}numbers[low] = temp;return low;}

最好时间复杂度:O(nlogn)

最坏时间复杂度:O(n2)

平均时间复杂度:O(nlogn)

1 0
原创粉丝点击