该算法的平均时间复杂度为nlog2n,空间复杂度为log2n,是一个不稳定的算法,一般排序元素个数在20个以上选择此算法为佳。
template <typename T>
void quick_sort(T table[MAX], int low, int high)
{
int i,j,temp;
int L_low,L_high,R_low,R_high;
i = low;
j = high;
temp = table[low];
while (i<j)
{
while(temp<=table[j]&&i<j)//直到找到比temp小的元素
j--;
table[i] = table[j];//比temp小的数据移到前面
while(temp>=table[i]&&i<j)//直到找到比temp大的元素
i++;
table[j] = table[i];//比temp大的数据移到后面
}
table[i] = temp;//temp被插入到排列后的最终位置
L_low = low;
L_high = i-1;
if(L_high>L_low)
quick_sort(table,L_low,L_high);
R_low = i+1;
R_high = high;
if(R_low<R_high)
quick_sort(table,R_low,R_high);
}