用c语言,对10个学生的信息按成绩进行降序排序
时间: 2024-03-09 12:51:08 浏览: 19
好的,我可以帮您实现这个功能。以下是使用选择排序算法对10个学生信息按成绩进行降序排序的示例代码:
```c
#include <stdio.h>
// 定义学生信息结构体
struct student {
char name[20];
int score;
};
// 选择排序函数
void select_sort(struct student stu[], int n) {
int i, j, max_idx;
struct student temp;
for (i = 0; i < n - 1; i++) {
max_idx = i;
for (j = i + 1; j < n; j++) {
if (stu[j].score > stu[max_idx].score) {
max_idx = j;
}
}
if (max_idx != i) {
temp = stu[i];
stu[i] = stu[max_idx];
stu[max_idx] = temp;
}
}
}
int main() {
struct student stu[10] = {
{"Tom", 88},
{"Jerry", 76},
{"Alice", 92},
{"Bob", 65},
{"Mike", 80},
{"John", 78},
{"Lucy", 85},
{"Lily", 90},
{"David", 70},
{"Frank", 82}
};
int i;
printf("排序前的学生信息:\n");
for (i = 0; i < 10; i++) {
printf("%s\t%d\n", stu[i].name, stu[i].score);
}
select_sort(stu, 10);
printf("\n排序后的学生信息:\n");
for (i = 0; i < 10; i++) {
printf("%s\t%d\n", stu[i].name, stu[i].score);
}
return 0;
}
```
输出结果如下:
```
排序前的学生信息:
Tom 88
Jerry 76
Alice 92
Bob 65
Mike 80
John 78
Lucy 85
Lily 90
David 70
Frank 82
排序后的学生信息:
Alice 92
Lily 90
Tom 88
Lucy 85
Frank 82
Mike 80
John 78
Jerry 76
David 70
Bob 65
```
希望这个示例代码能对您有所帮助。