#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<<L.r[i].name<<" "<<L.r[i].Chinese<<" "<<L.r[i].Math<<" "<<L.r[i].English<<endl;
}
cout<<endl;
}
//堆排序算法
//堆排序算法实现 退出堆顶元素后建立新堆
typedef Sqlist HeapType;
void HeapAdjust (HeapType& H,int s,int m){
student rc=H.r[s];
for(int j=2*s;j<=m;j*=2){
if(j<m&&H.r[j].key>H.r[j+1].key)
j=j+1;
if(rc.key<=H.r[j].key)
break;
H.r[s]=H.r[j];
s=j;
}
H.r[s]=rc;
}
//对表进行堆排序,将无序序列构建成一个堆
void HeapSort(HeapType& H){
for(int i=H.length/2;i>0;i--)
HeapAdjust (H,i,H.length);
for(int i=H.length;i>1;i--){
H.r[0]= H.r[1];
H.r[1]=H.r[i];
H.r[i]=H.r[0];//将堆顶记录与最后一个元素交换
HeapAdjust (H,1,i-1);//再重新调整 因为堆顶元素不满足堆的性质了
}
}