#define maxsize 3
typedef struct{
string name;
int Math;
int English;
int Chinese;
int key;
}student;
typedef struct{
student r[maxsize+1];
int length;
}Sqlist;
//初始化线性表,给线性表赋值
void creatlist(Sqlist& L){
L.length=0;
for(int i=1;i<=maxsize;i++){
cin>>L.r[i].name>>L.r[i].Chinese>>L.r[i].Math>>L.r[i].English;
L.r[i].key=L.r[i].Chinese+L.r[i].Math+L.r[i].English;
L.length++;
}
}
//输出线性表
void show(Sqlist L){
for(int i=1;i<=L.length;i++){
cout<<"第"<<i<<"名为:";
cout<<L.r[i].name<<" "<<L.r[i].Chinese<<" "<<L.r[i].Math<<" "<<L.r[i].English<<endl;
}
cout<<endl;
}
//每一次的快速排序
int partition(Sqlist& L,int low,int high){
L.r[0] = L.r[low];
int pivotkey = L.r[low].key;
while(low<high){
while((low<high)&&(L.r[high].key<=pivotkey))
high--;
L.r[low] = L.r[high];
while((low<high)&&(L.r[low].key>=pivotkey))
low++;
L.r[high] = L.r[low];
}
L.r[low] = L.r[0];//枢轴的位置(值)
return low; //返回枢轴
}
//快速排序递归算法
void Qsort(Sqlist& L,int low ,int high){
if(low<high){
int pivotloc = partition(L,low,high);
Qsort(L,low,pivotloc-1);
Qsort(L,pivotloc+1,high);
}
}
在main函数中调用
int main(){
Sqlist b;
creatlist(b);
int low=1;
int high=b.length;
//show(b);
Qsort(b,low,high);
show(b);
}