学习笔记
编译环境;vc6.0
#include <stdio.h>
//*********************快速排序******************
int Partition(int a[], int low, int high)
{
int pivotkey; //pivotkey枢轴关键字
pivotkey = a[low]; //用第一个数作为枢轴
a[0] = pivotkey; //a[0]保存pivotkey的值
while (low < high) //两端交替向中间扫描
{
while (low < high && a[high] >= pivotkey)
high--;
a[low] = a[high];
while (low < high && a[low] <= pivotkey)
low++;
a[high] = a[low];
} //low==high时结束
a[low] = a[0]; //将枢轴值替换回a[low]
return low; //返回枢轴所在位置
}
//*********************快速排序******************
void Qsort(int a[], int low, int high)
{
int pivotkey; //枢轴
if (low < high)
{
pivotkey = Partition(a, low, high); //pivotkey将数组a一分为二
Qsort(a, low, pivotkey - 1); //对低子数组递归排序
Qsort(a, pivotkey + 1, high); //对高子数组递归排序
}
}
int main()
{
int i;
int a[12] = { 0,423,2,6,34,543,23,53,27,254,9,5 }; //a[0]保存pivotkey的值
Qsort(a, 1, 11); //排序
for (i = 1; i < 12; i++) //输出
printf("%d\t", a[i]);
printf("\n");
}
结果