void Sort_CollegeScore(AllCollege *as) //按学院总分排序(直接插入排序) { int i,j,k; printf("\t|----------------------------------------------------------------|\n"); printf("\t|---- 学院编号\t|学院名字 |男团体总分|女团体总分|总分 |----|\n"); printf("\t|----------------------------------------------------------------|\n"); for (i = 2;i<as->College_num;i++) { as->College[0].boys_score = as->College[i].boys_score; as->College[0].girl_score = as->College[i].girl_score; as->College[0].shool_score = as->College[i].shool_score; as->College[0].College = as->College[i].College; strcpy(as->College[0].College_name,as->College[i].College_name); j = i - 1; while (as->College[0].shool_score < as->College[j].shool_score && j >0) { as->College[j+1].boys_score = as->College[j].boys_score; as->College[j+1].girl_score = as->College[j].girl_score; as->College[j+1].shool_score = as->College[j].shool_score; as->College[j+1].College = as->College[j].College; strcpy(as->College[j+1].College_name,as->College[j].College_name); j--; } as->College[j+1].boys_score = as->College[0].boys_score; as->College[j+1].girl_score = as->College[0].girl_score; as->College[j+1].shool_score = as->College[0].shool_score; as->College[j+1].College = as->College[0].College; strcpy(as->College[j+1].College_name,as->College[0].College_name); } for (k = 2;k<=as->College_num;k++) { printf("\t|---- %-9d|%-8s |%-4d\t|%-10d|%-8d|----|\n",as->College[k].College,as->College[k].College_name,as->College[k].boys_score,as->College[k].girl_score,as->College[k].shool_score); printf("\t|----------------------------------------------------------------|\n"); } printf("\n\n");system("pause"); }
时间: 2023-09-16 17:12:06 浏览: 98
这段代码是使用直接插入排序算法对一个结构体数组按照学院总分进行排序,并输出排序后的结果。具体实现过程如下:
1. 首先输出一个表格的表头。
2. 从第二个元素开始,将当前元素存储到一个临时变量as->College[0]中,然后在前面已经排好序的部分中,从后往前遍历,找到插入位置。
3. 将当前元素后面的所有元素向后移动一个位置,腾出插入位置。
4. 将临时变量as->College[0]中存储的元素插入到插入位置。
5. 输出排序后的结果。
这段代码的时间复杂度为O(n^2),在数据量较小的情况下可以使用。如果数据量较大,建议使用更高效的排序算法,例如快速排序或归并排序。
相关问题
void sort_student(); // 按总分排序学生信息,用链表,用c语言
好的,这是一个按照学生总分排序的C语言程序,使用了链表数据结构实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 学生信息结构体
struct student {
char name[20];
int chinese;
int math;
int english;
int total;
struct student *next;
};
// 创建学生信息链表
struct student *create_student_list() {
struct student *head, *tail, *p;
head = tail = NULL;
char name[20];
int chinese, math, english;
while (1) {
printf("请输入学生姓名(输入0结束):");
scanf("%s", name);
if (strcmp(name, "0") == 0) {
break;
}
printf("请输入语文成绩:");
scanf("%d", &chinese);
printf("请输入数学成绩:");
scanf("%d", &math);
printf("请输入英语成绩:");
scanf("%d", &english);
// 计算总分并创建新节点
p = (struct student*)malloc(sizeof(struct student));
strcpy(p->name, name);
p->chinese = chinese;
p->math = math;
p->english = english;
p->total = chinese + math + english;
p->next = NULL;
// 把新节点插入到链表尾部
if (head == NULL) {
head = tail = p;
} else {
tail->next = p;
tail = p;
}
}
return head;
}
// 按照学生总分排序链表
void sort_student(struct student *head) {
struct student *p, *q, *temp;
int flag;
for (p = head; p != NULL; p = p->next) {
flag = 0;
for (q = head; q->next != NULL; q = q->next) {
if (q->total < q->next->total) {
temp = q->next;
q->next = temp->next;
temp->next = q;
if (q == head) {
head = temp;
} else {
p->next = temp;
}
flag = 1;
}
}
if (flag == 0) {
break;
}
}
}
// 打印学生信息
void print_student(struct student *head) {
struct student *p;
printf("姓名\t语文\t数学\t英语\t总分\n");
for (p = head; p != NULL; p = p->next) {
printf("%s\t%d\t%d\t%d\t%d\n", p->name, p->chinese, p->math, p->english, p->total);
}
}
int main() {
struct student *head;
head = create_student_list();
sort_student(head);
print_student(head);
return 0;
}
```
这个程序会先让用户输入学生信息,包括姓名、语文、数学、英语成绩,然后计算总分并创建一个新节点,把新节点插入到链表尾部。
接着,程序会按照学生总分从大到小排序链表,使用冒泡排序算法实现。最后,程序会打印出排序后的学生信息列表。
#include <stdio.h> #include <string.h> #define MAX_STUDENT_NUM 30 //最大学生个数 #define MAX_COURSE_NUM 6 //最大课程个数 #define NAME_LEN 20 //最大名字个数 typedef struct Student { char id[10];//学生学号 char name[NAME_LEN];//姓名 int scores[MAX_COURSE_NUM];//各科分数 int total_score;//总分 float avg_score;//平均分 } XUESHENG;//将结构体名称命名为XUESHENG void sort_by_total_score(XUESHENG students[], int num);//按每个学生的总分由高到低排出并输出名次表 void sort_by_number(XUESHENG students[], int num);//按学号由小到大排出并输出成绩表 void sort_by_name(XUESHENG students[], int num);//按姓名的字典顺序排出并输出成绩表 请补充各个函数
函数1:按每个学生的总分由高到低排出并输出名次表
```
void sort_by_total_score(XUESHENG students[], int num){
int i, j, rank = 1;
XUESHENG temp;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(students[i].total_score < students[j].total_score){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("名次\t学号\t姓名\t总分\t平均分\n");
for(i = 0; i < num; i++){
printf("%d\t%s\t%s\t%d\t%.2f\n", rank++, students[i].id, students[i].name, students[i].total_score, students[i].avg_score);
}
}
```
函数2:按学号由小到大排出并输出成绩表
```
void sort_by_number(XUESHENG students[], int num){
int i, j;
XUESHENG temp;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(strcmp(students[i].id, students[j].id) > 0){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("学号\t姓名\t");
for(i = 0; i < MAX_COURSE_NUM; i++){
printf("科目%d\t", i+1);
}
printf("总分\t平均分\n");
for(i = 0; i < num; i++){
printf("%s\t%s\t", students[i].id, students[i].name);
for(j = 0; j < MAX_COURSE_NUM; j++){
printf("%d\t", students[i].scores[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
```
函数3:按姓名的字典顺序排出并输出成绩表
```
void sort_by_name(XUESHENG students[], int num){
int i, j;
XUESHENG temp;
for(i = 0; i < num - 1; i++){
for(j = i + 1; j < num; j++){
if(strcmp(students[i].name, students[j].name) > 0){
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("学号\t姓名\t");
for(i = 0; i < MAX_COURSE_NUM; i++){
printf("科目%d\t", i+1);
}
printf("总分\t平均分\n");
for(i = 0; i < num; i++){
printf("%s\t%s\t", students[i].id, students[i].name);
for(j = 0; j < MAX_COURSE_NUM; j++){
printf("%d\t", students[i].scores[j]);
}
printf("%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
```
阅读全文