-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.c
More file actions
51 lines (44 loc) · 1.02 KB
/
Copy pathSort.c
File metadata and controls
51 lines (44 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
struct Student
{
int num;
char name[20];
float score;
};
int main()
{
struct Student stu[5] = {
{10101, "Zhang", 78},
{10102, "Wang", 85},
{10103, "Li", 92},
{10104, "Zhao", 65},
{10105, "Qian", 88}
};
// 选择排序:按成绩降序排列
int i, j, k;
struct Student temp;
for(i = 0; i < 4; i++) // n-1轮比较
{
k = i; // 记录当前最高成绩的位置
for(j = i + 1; j < 5; j++)
{
if(stu[j].score > stu[k].score)
k = j; // 更新最高成绩的位置
}
// 交换位置
if(k != i)
{
temp = stu[i];
stu[i] = stu[k];
stu[k] = temp;
}
}
// 输出排序结果
printf("学号\t姓名\t成绩\n");
printf("-------------------\n");
for(i = 0; i < 5; i++)
{
printf("%d\t%s\t%.1f\n", stu[i].num, stu[i].name, stu[i].score);
}
return 0;
}