目前为止的三种排序:直接插入排序、冒泡排序和简单选择排序都是o(
n
2
n^2
n2)的运行时间。虽然冒泡加入标志位优化和直接插入排序都可以在最好情况下为o(
n
n
n),但是在最坏情况和平均情况下,性能还是o(
n
2
n^2
n2)。
还有复杂度为o(nlogn)的更好算法,这些算法的秘诀就是分而治之的策略。本文介绍面试中一定盲写都要会写的快速排序。
快速排序属于交换排序的一种。
算法思想
快排的基本思想是基于分治法的:在待排序表L[1…n]中任取一元素pivot作为基准(通常取首元素),通过一趟排序将待排序表划分为独立的两部分L[1…k-1]和L[k+1…n],使得L[1…k-1]中的所有元素都小于pivot,L[k+1…n]中的所有元素都大于等于pivot,则pivot被放在了其最终位置L(k)上,这个过程成为一趟快速排序(或一次划分)。然后分别递归的对两个子表重复上述过程,直至每部分内只有一个元素或空为止,即所有元素放在了其最终位子上。
图解如下:
上图中,演示了快速排序的处理过程:
初始状态为一组无序的数组:2、4、5、1、3。
经过以上操作步骤后,完成了第一趟排序,得到新的数组:1、2、5、4、3。
新的数组中,以2为分割点,左边都是比2小的数,右边都是比2大的数。
因为2已经在数组中找到了合适的位置,所以不用再动。
2左边的数组只有一个元素1,所以显然不用再排序,位置也被确定。(注:这种情况时,left指针和right指针显然是重合的。因此在代码中,我们可以通过设置判定条件left必须小于right,如果不满足,则不用排序了)。
而对于2右边的数组5、4、3,设置left指向5,right指向3,开始继续重复图中的一、二、三、四步骤,对新的数组进行排序。
实现
Python实现:
def swap(lyst:list,i:int,j:int)->None:
tmp = lyst[i]
lyst[i] = lyst[j]
lyst[j] = tmp
def partition(lyst:list, left:int,right:int)->int:
middle = (left+right)//2
pivot = lyst[middle]
lyst[middle] = lyst[right]
lyst[right] = pivot
boundary = left
for index in range(left,right):
if lyst[index] < pivot:
swap(lyst,index,boundary)
boundary += 1
swap(lyst,right,boundary)
return boundary
def quickSortHelper(lyst: list,left:int,right:int) ->None:
if left<right:
pivotLocation = partition(lyst,left,right)
quickSortHelper(lyst,left,pivotLocation-1)
quickSortHelper(lyst,pivotLocation+1,right)
def quickSort(lyst: list)->None:
quickSortHelper(lyst,0,len(lyst)-1)
lyst = [9,7,2,5,5,4,8,6,3,5]
quickSort(lyst)
print(lyst)
C++实现:
#include <iostream>
#include <vector>
using namespace std;
void swap(vector<int>& lyst, int i,int j){
int tmp = lyst[i];
lyst[i] = lyst[j];
lyst[j] = tmp;
}
int partition(vector<int>& lyst,int left,int right){
int pivot = lyst[left];
lyst[left] = lyst[right];
lyst[right] = pivot;
int boundary = left;
for(int index=left;index<right;index++){
if(lyst[index]<pivot){
swap(lyst,index,boundary);
boundary ++;
}
}
swap(lyst,right,boundary);
return boundary;
}
void quickSortHelper(vector<int>& lyst,int left,int right){
if(left<right){
int pivotLocation = partition(lyst,left,right);
quickSortHelper(lyst,left,pivotLocation-1);
quickSortHelper(lyst,pivotLocation+1,right);
}
}
vector<int> quickSort(vector<int>& lyst){
quickSortHelper(lyst,0,lyst.size()-1);
return lyst;
}
int main() {
vector<int> test = {9,4,6,7,2,8,2};
vector<int> res = quickSort(test);
for (int i = 0; i < res.size(); i++){
cout << res[i] << " ";
}
return 0;
}
算法分析
时间: 快排的运行时间与划分是否对称有关,快排的最坏情况发生在两个区域分别包含n-1个元素和0个元素时,即对应排序表基本有序或基本逆序时,就得到最坏情况下的时间复杂度o(
n
2
n^2
n2)。
最理想的情况是,partition尽可能做到最平衡的划分,得到的两个子问题的大小都不可能大于n/2,这种情况下,快排的运行速度大大提升为o(
n
l
o
g
2
n
nlog_2n
nlog2n)。
快排也是所有内部排序中平均性能最优的算法。
空间: 由于快排是递归的,因此需要借助一个递归工作栈来保存每层递归调用的必要信息,其容量应与递归调用的最大深度一致,最好情况下为o(
l
o
g
2
n
log_2n
log2n),最坏为o(
n
n
n)。平均情况下,栈深度为o(
l
o
g
2
n
log_2n
log2n)。
稳定性: 在划分算法中,若右端区间有两个关键字相同,且均小于基准值的记录,则在交换到左端空间后,相对位置会发生改变。如表L[3,2*,2]。
选择pivot的其他方法可以是选择一个随机位置,或者选择第一个元素、中间元素和最后元素之间的中位数的位置
快排的优化1:当递归的数据规模充分小,则停止递归。直接调用简单排序比如插入排序,即定义cutoff的阈值。因为对于待排序的序列长度很小或是基本趋于有序时,还是插排效率好。
快排的优化2: 在一次排序后,可以将与基准值相等的数放在一起,在下次分割时可以不考虑这些数。具体可参考https://blog.youkuaiyun.com/lvyibin890/article/details/79044068。
参考链接:https://cuijiahua.com/blog/2017/12/algorithm_4.html