快速排序属于“交换类”的算法,和冒泡算法一样,比冒泡算法更高效
其C语言代码如下:
void QuickSort(int A[], int low,int high)
{
int i,j,temp;
i = low;
j = high;
if(low < high)
{
temp = A[low];
while(low < high)
{
while(j > i && A[j] >= temp)
--j;
temp = A[i];
A[i] = A[j];
A[j] = temp;
while(j > i && A[i] <= temp)
++i;
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
A[i] = temp;
QuickSort(A,low,i-1);
QuickSort(A.i+1,high);
}
}
快速排序算法突破了排序算法时间复杂度普遍为O(n^2)的瓶颈,时间复杂度来到了O(n*log2 n),但是并不是一种稳定的算法。