数据结构-快速排序
1、注意
不能忽略等号(见下文),会导致死循环,有相等的即填坑
2、代码
// 交换函数
void swap(int k[], int low, int high)
{
int temp;
temp = k[low];
k[low] = k[high];
k[high] = temp;
}
int Partition(int k[], int low, int high)
{
int point;
point = k[low];
while (low < high)
{
//过滤掉所有比基准点大的,这里一定要加=
while (low < high && k[high] >= point)
{
high--;
}
//交换
swap(k, low, high);
//过滤掉所有比基准点小的
while (low < high && k[low] <= point)
{
low++;
}
swap(k, low, high);
}
}
void QSort(int k[], int low, int high)
{
int point;
if (low < high)
{
point = Partition(k, low, high);
QSort(k, low, point - 1);
QSort(k, point + 1, high);
}
}
void QuickSort(int k[], int n)
{
QSort(k, 0, n - 1);
}