定义一个结构体学生,包括学号,性别,姓名和成绩四个成员属性,然后定义长度为3的一个学生类型的数组,对

定义一个结构体学生,包括学号,性别,姓名和成绩四个成员属性,然后定义长度为3的一个学生类型的数组,对其赋值后按分数降序排序,最后输出。
#include<stdio.h>
#define N 3
struct student
{ int num;
char sex;
char name[20];
float score;
};
struct student stu[3]={{12251,'M',"Zhang chun",85},{12252,'F',"Li hua",89},{12253,'M',"Xie yao",98}};

void main()
{ struct student temp;
int i,j,k;
printf("The order is: \n");
for(i=0;i<N-1;i++)
{ k=i;
for(j=i+1;j<N;j++)
if(stu[j].score>stu[k].score)
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;
}
for(i=0;i<N;i++)
printf("%d %c %s %f\n",stu[i].num,stu[i].sex,stu[i].name,stu[i].score);
printf("\n");
}

为什么不可以写成#include<stdio.h>
#define N 3
struct student
{ int num;
char sex;
char name[20];
float score;
};
struct student stu[3]={{12251,'M',"Zhang chun",85},{12252,'F',"Li hua",89},{12253,'M',"Xie yao",98}};

void main()
{ struct student temp;
int i,j,k;
printf("The order is: \n");
for(i=0;i<N-1;i++)
{
for(j=i+1;j<N;j++)
if(stu[j].score>stu[i].score)

temp=stu[j];stu[j]=stu[i];stu[i]=temp;
}
for(i=0;i<N;i++)
printf("%d %c %s %f\n",stu[i].num,stu[i].sex,stu[i].name,stu[i].score);
printf("\n");
}
这是标准胡段的冒泡算法裤虚誉誉磨,每次需将最大节点排到高位。
第二种破坏了算法。
在if(stu[j].score>stu[i].score)
temp=stu[j];stu[j]=stu[i];stu[i]=temp;时改变了待比较的stu[i]为较小的数值。
第一种写法就是保持该值不变。