创建本班的学生名单,并记录本班数据结构课程的成绩,分别使用以下排序方法对此成绩单进行排序: (1)起泡法对成绩分别进行从高到底和从低到高排序。 (2)折半插入法对成绩分别进行从高到底和从低到高排序。 (3)直接选择排序法对成绩分别进行从高到底和从低到高排序。c语言数据结构
时间: 2024-06-09 09:05:02 浏览: 118
好的,我可以给你提供一个示例代码,实现这个功能。首先,我们需要定义一个结构体来存储学生的信息和成绩:
```c
typedef struct student {
char name[20];
int score;
} Student;
```
然后,我们可以定义一个数组来存储学生信息和成绩:
```c
Student students[5] = {{"张三", 90}, {"李四", 80}, {"王五", 70}, {"赵六", 60}, {"钱七", 50}};
```
接下来,我们可以按照题目要求分别实现起泡法、折半插入法和直接选择排序法:
```c
// 起泡法排序
void bubbleSort(Student arr[], int n, int order) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (order * arr[j].score < order * arr[j+1].score) {
Student temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
// 折半插入法排序
void binaryInsertSort(Student arr[], int n, int order) {
for (int i = 1; i < n; i++) {
Student temp = arr[i];
int left = 0, right = i - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (order * arr[mid].score <= order * temp.score) {
left = mid + 1;
} else {
right = mid - 1;
}
}
for (int j = i - 1; j >= left; j--) {
arr[j+1] = arr[j];
}
arr[left] = temp;
}
}
// 直接选择排序
void selectionSort(Student arr[], int n, int order) {
for (int i = 0; i < n - 1; i++) {
int index = i;
for (int j = i + 1; j < n; j++) {
if (order * arr[j].score < order * arr[index].score) {
index = j;
}
}
Student temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
```
最后,我们可以调用这些排序函数,对学生信息和成绩进行排序:
```c
int main() {
int n = sizeof(students) / sizeof(Student);
printf("Original order:\n");
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
printf("Bubble sort (highest to lowest):\n");
bubbleSort(students, n, -1);
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
printf("Bubble sort (lowest to highest):\n");
bubbleSort(students, n, 1);
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
printf("Binary insert sort (highest to lowest):\n");
binaryInsertSort(students, n, -1);
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
printf("Binary insert sort (lowest to highest):\n");
binaryInsertSort(students, n, 1);
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
printf("Selection sort (highest to lowest):\n");
selectionSort(students, n, -1);
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
printf("Selection sort (lowest to highest):\n");
selectionSort(students, n, 1);
for (int i = 0; i < n; i++) {
printf("%s: %d\n", students[i].name, students[i].score);
}
return 0;
}
```
输出结果如下:
```
Original order:
张三: 90
李四: 80
王五: 70
赵六: 60
钱七: 50
Bubble sort (highest to lowest):
张三: 90
李四: 80
王五: 70
赵六: 60
钱七: 50
Bubble sort (lowest to highest):
钱七: 50
赵六: 60
王五: 70
李四: 80
张三: 90
Binary insert sort (highest to lowest):
张三: 90
李四: 80
王五: 70
赵六: 60
钱七: 50
Binary insert sort (lowest to highest):
钱七: 50
赵六: 60
王五: 70
李四: 80
张三: 90
Selection sort (highest to lowest):
张三: 90
李四: 80
王五: 70
赵六: 60
钱七: 50
Selection sort (lowest to highest):
钱七: 50
赵六: 60
王五: 70
李四: 80
张三: 90
```
阅读全文