#include<stdio.h>
#include<stdlib.h>
struct name
{ int xuehao ;//xuehao
int score;
}s[100];
void swap(name &s1,name &s2)
{ name temp;
temp.xuehao=s1.xuehao;
temp.score=s1.score;
s1.xuehao=s2.xuehao;
s1.score=s2.score;
s2.xuehao=temp.xuehao;
s2.score=temp.score;
}
void paixu(name *s,int k)
{ if(k>1)
{
int info=s[0].score;
int key_1=k-1;
int key_2=0;
int key_control=0;
while(key_1!=key_2)
{
if(key_control==0)
{
if(s[key_1].score<info)
{
swap(s[key_1],s[key_2]);
key_2++;
key_control=1;
}
else
key_1--;
continue;
}
if(key_control==1)
{
if(s[key_2].score>info)
{
swap(s[key_2],s[key_1]);
key_1--;
key_control=0;
}
else
key_2++;
}
//paixu(s,key_1);
//paixu(s+key_1+1,k-key_1-1);
}
}
}
int main()
{ int k;
printf("请输入学生的人数!\n");
scanf("%d",&k);
for(int i=0;i<k;i++)
{
printf("请输入第%d个学生的学号和得分\n",i+1);
scanf("%ld",&s[i].xuehao);
scanf("%d",&s[i].score);
}
paixu(s,k);
for( int j=0;j<k;j++)
{
printf("第%d个学生的学号和得分 ",j+1);
printf("%ld ",s[j].xuehao);
printf("%d\n",s[j].score);
}
return 0;
}
结果不能正确输出,测试数据为 49 38 65 97 76 13 27 49 结果为 13 27 39 49 76 49 65 97