快速排序(东软喜欢考类似的算法填空题,又如堆排序的算法等)
【正确答案】#include "stdafx.h"
#define N 10
int part(int list[], int low, int high) // 一趟排序,返回分割点位置
{
int tmp = list[low];
while(low<high){
while(low<high && list[high]>=tmp) --high;
list[low] = list[high];
while(low<high && list[low]<=tmp) ++low;
list[high] = list[low];
}
list[low] = tmp;
return low;
}

void QSort(int list[], int low, int high) // 应用递归进行快速排序
{
if(low<high){
int mid = part(list, low, high);
QSort(list, low, mid-1);
QSort(list, mid+1, high);
}
}

void show(int list[], int n) // 输出列表中元素
{
for(int i=0; i<n; i++)
printf("%d ", list[i]);
printf("/n");
}
int main(int argc, char* argv[])
{
int list[N] = {23, 65, 26, 1, 6, 89, 3, 12, 33, 8};
show(list, N); // 输出排序前序列
QSort(list, 0, N-1); // 快速排序
show(list, N); // 输出排序后序列
return getchar();
}
【答案解析】