关于排序的问题
有什么简单的方法可以将一组数从大到小排列?
冒泡和选择啊
程序代码:
root@~ #cat jp.c
#include <stdio.h>
#include <string.h>
//定义结构体
struct stu {
char name[10];
int score;
};
int main (void) {
struct stu boy[100];//声明一个100个学生的结构变量
int n,i,j,temp; //程序所需变量
char tmp[10];
//提示输入学生人数
printf("Enter numbers of students:");
scanf("%i",&n);
//开始输入学生名和成绩
for(i=0;i<n;i++) {
scanf("%s%i",boy[i].name,&boy[i].score);
}
//开始对成绩降序排序
for(i=0;i<n-1;i++) {
for(j=i+1;j<n;j++) {
if(boy[i].score<boy[j].score) {
temp=boy[i].score;
boy[i].score=boy[j].score;
boy[j].score=temp;
strcpy(tmp,boy[i].name);
strcpy(boy[i].name,boy[j].name);
strcpy(boy[j].name,tmp);
}
}
}
printf("After Sort ...\n\n");
//打印结果
for(i=0;i<n;i++) printf("%s %i\n",boy[i].name,boy[i].score);
return 0;
}
root@~ #
程序代码:root@~ #./jp Enter numbers of students:5 zhang1 90 zhang2 91 zhang3 99 zhang4 91 zhang5 98 After Sort ... zhang3 99 zhang5 98 zhang4 91 zhang2 91 zhang1 90 root@~ #
