如何用c语言表述出按学号递增排序和按总分递减排序。
时间: 2024-02-05 14:10:55 浏览: 74
按学号递增排序可以使用冒泡排序或者快速排序等算法实现,以下是冒泡排序的示例代码:
```c
struct student {
int id;
int total_score;
};
void sort_by_id(struct student *students, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].id > students[j + 1].id) {
struct student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
```
按总分递减排序可以使用快速排序等算法实现,以下是快速排序的示例代码:
```c
void sort_by_total_score(struct student *students, int left, int right) {
if (left >= right) {
return;
}
int i = left, j = right;
int pivot = students[(left + right) / 2].total_score;
while (i <= j) {
while (students[i].total_score > pivot) {
i++;
}
while (students[j].total_score < pivot) {
j--;
}
if (i <= j) {
struct student temp = students[i];
students[i] = students[j];
students[j] = temp;
i++;
j--;
}
}
sort_by_total_score(students, left, j);
sort_by_total_score(students, i, right);
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)