1.以第1个元素作为基准
#include
#define MaxSize 20
typedef int KeyType; //定义关键字类型
typedef char InfoType[10];
typedef struct //记录类型
{
KeyType key; //关键字项
InfoType data; //其他数据项,类型为InfoType
} RecType; //排序的记录类型定义
void QuickSort(RecType R[],int s,int t) //对R[s]至R[t]的元素进行快速排序
{
int i=s,j=t;
RecType tmp;
if (si && R[j].key>=tmp.key)
j--; //从右向左扫描,找第1个小于tmp.key的R[j]
R[i]=R[j]; //找到这样的R[j],R[i]"R[j]交换
while (i
运行结果:
2.以中间位置的元素作为基准
#include
#define MaxSize 20
typedef int KeyType; //定义关键字类型
typedef char InfoType[10];
typedef struct //记录类型
{
KeyType key; //关键字项
InfoType data; //其他数据项,类型为InfoType
} RecType; //排序的记录类型定义
void QuickSort1(RecType R[],int s,int t) //对R[s]至R[t]的元素进行快速排序
{
int i=s,j=t;
KeyType pivot;
RecType tmp;
pivot = R[(s+t)/2].key; //用区间的中间位置的元素作为关键字
if (si && R[j].key>pivot)
j--; //从右向左扫描,找第1个小于基准的R[j]
while (i
运行结果: