#include<iostream>
using namespace std;
#define max 20
int i,j,k,low,high,p,pk;
typedef struct {
int key;
int otherwise;
} type;
typedef struct { //定义结构体
type a[max];
int length;
} sqlist;
void creatsqlist(sqlist &S) { //输入排顺序的数字
printf("输入排序数字长度:\n");cin>>S.length;
printf("输入排序数字:\n");
for(i=1; i<=S.length; i++)
cin>>S.a[i].key;
}
int partion(sqlist &S,int low,int high) {
S.a[0]=S.a[low]; //S.a[0]暂时存放轴的值
p=S.a[low].key; //记录保存轴的值
while(low<high) {
while(low<high&&S.a[high].key>=p) high--; //向左寻找第一个小于轴的数值
S.a[low]=S.a[high];
while(low<high&&S.a[low].key<=p)low++; //向右寻找第一个小于轴的数值
S.a[high]=S.a[low];
}
S.a[low]=S.a[0];
return low;
}
void Qsort(sqlist &S,int low,int high) {
//对顺序表的子序列S.a[low...high]快速排序
if(low<high) { //长度大于1
pk=partion(S,low,high);
Qsort(S,low,pk-1); //快速排序轴左侧
Qsort(S,pk+1,high); //快速排序轴右侧
}
}
void printf(sqlist S) { //输出排序后的数字
printf("快速排序结果为:\n");
for(i=1; i<=S.length; i++)
printf("%d ",S.a[i].key);
printf("\n");
}
void Quicksort(sqlist &S) {
Qsort(S,1,S.length);
}
int main() {
sqlist S;
creatsqlist(S);
Quicksort(S);
printf(S);
}