C语言代码,分别用简单选择排序,快速排序,冒泡排序,折半插入排序,直接插入排序。(2)输出格式:依次输出按照不同关键字排序后的结果: Ø C=1时,按照姓名的非递减字典序排序; Ø C=2时,按照学号递增排序; Ø C=3时,按照成绩的非递减排序。 (3)测试用例: 序号 输入 输出 说明 1 初始数据为: James 00007 85 Amy 00010 90 Zoe 00001 60 Tom 00005 85 Tom 00015 70
时间: 2024-03-21 11:43:42 浏览: 21
好的,这是一个排序问题,我可以帮你解答。以下是 C 语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
typedef struct{
char name[20];
char id[20];
int score;
}Student;
void swap(Student *a, Student *b)
{
Student temp = *a;
*a = *b;
*b = temp;
}
void selectSort(Student stu[], int n, int c)
{
int i, j, k;
for(i = 0; i < n - 1; i++)
{
k = i;
for(j = i + 1; j < n; j++)
{
if(c == 1)
{
if(strcmp(stu[j].name, stu[k].name) < 0)
{
k = j;
}
}
else if(c == 2)
{
if(strcmp(stu[j].id, stu[k].id) < 0)
{
k = j;
}
}
else if(c == 3)
{
if(stu[j].score < stu[k].score)
{
k = j;
}
}
}
if(k != i)
{
swap(&stu[i], &stu[k]);
}
}
}
void quickSort(Student stu[], int left, int right, int c)
{
int i, j;
Student pivot;
if(left < right)
{
i = left;
j = right;
pivot = stu[left];
while(i < j)
{
if(c == 1)
{
while(i < j && strcmp(stu[j].name, pivot.name) >= 0)
{
j--;
}
if(i < j)
{
stu[i++] = stu[j];
}
while(i < j && strcmp(stu[i].name, pivot.name) <= 0)
{
i++;
}
if(i < j)
{
stu[j--] = stu[i];
}
}
else if(c == 2)
{
while(i < j && strcmp(stu[j].id, pivot.id) >= 0)
{
j--;
}
if(i < j)
{
stu[i++] = stu[j];
}
while(i < j && strcmp(stu[i].id, pivot.id) <= 0)
{
i++;
}
if(i < j)
{
stu[j--] = stu[i];
}
}
else if(c == 3)
{
while(i < j && stu[j].score >= pivot.score)
{
j--;
}
if(i < j)
{
stu[i++] = stu[j];
}
while(i < j && stu[i].score <= pivot.score)
{
i++;
}
if(i < j)
{
stu[j--] = stu[i];
}
}
}
stu[i] = pivot;
quickSort(stu, left, i - 1, c);
quickSort(stu, i + 1, right, c);
}
}
void bubbleSort(Student stu[], int n, int c)
{
int i, j;
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(c == 1)
{
if(strcmp(stu[j].name, stu[j+1].name) > 0)
{
swap(&stu[j], &stu[j+1]);
}
}
else if(c == 2)
{
if(strcmp(stu[j].id, stu[j+1].id) > 0)
{
swap(&stu[j], &stu[j+1]);
}
}
else if(c == 3)
{
if(stu[j].score > stu[j+1].score)
{
swap(&stu[j], &stu[j+1]);
}
}
}
}
}
void binaryInsertSort(Student stu[], int n, int c)
{
int i, j, left, right, mid;
for(i = 1; i < n; i++)
{
left = 0;
right = i - 1;
mid = 0;
if(c == 1)
{
while(left <= right)
{
mid = (left + right) / 2;
if(strcmp(stu[mid].name, stu[i].name) <= 0)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
}
else if(c == 2)
{
while(left <= right)
{
mid = (left + right) / 2;
if(strcmp(stu[mid].id, stu[i].id) <= 0)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
}
else if(c == 3)
{
while(left <= right)
{
mid = (left + right) / 2;
if(stu[mid].score <= stu[i].score)
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
}
for(j = i - 1; j >= left; j--)
{
stu[j+1] = stu[j];
}
stu[left] = stu[i];
}
}
void insertSort(Student stu[], int n, int c)
{
int i, j;
for(i = 1; i < n; i++)
{
if(c == 1)
{
for(j = i - 1; j >= 0 && strcmp(stu[j].name, stu[j+1].name) > 0; j--)
{
swap(&stu[j], &stu[j+1]);
}
}
else if(c == 2)
{
for(j = i - 1; j >= 0 && strcmp(stu[j].id, stu[j+1].id) > 0; j--)
{
swap(&stu[j], &stu[j+1]);
}
}
else if(c == 3)
{
for(j = i - 1; j >= 0 && stu[j].score > stu[j+1].score; j--)
{
swap(&stu[j], &stu[j+1]);
}
}
}
}
int main()
{
Student stu[MAXSIZE];
int n = 5;
int i, c, j;
strcpy(stu[0].name, "James");
strcpy(stu[0].id, "00007");
stu[0].score = 85;
strcpy(stu[1].name, "Amy");
strcpy(stu[1].id, "00010");
stu[1].score = 90;
strcpy(stu[2].name, "Zoe");
strcpy(stu[2].id, "00001");
stu[2].score = 60;
strcpy(stu[3].name, "Tom");
strcpy(stu[3].id, "00005");
stu[3].score = 85;
strcpy(stu[4].name, "Tom");
strcpy(stu[4].id, "00015");
stu[4].score = 70;
printf("请选择排序方式(1为简单选择排序,2为快速排序,3为冒泡排序,4为折半插入排序,5为直接插入排序):");
scanf("%d", &c);
switch(c)
{
case 1:
selectSort(stu, n, 1);
break;
case 2:
quickSort(stu, 0, n - 1, 2);
break;
case 3:
bubbleSort(stu, n, 3);
break;
case 4:
binaryInsertSort(stu, n, 1);
break;
case 5:
insertSort(stu, n, 2);
break;
default:
printf("输入错误\n");
return -1;
}
printf("排序后的结果:\n");
for(i = 0; i < n; i++)
{
printf("%s %s %d\n", stu[i].name, stu[i].id, stu[i].score);
}
return 0;
}
```
以上代码包括了简单选择排序、快速排序、冒泡排序、折半插入排序和直接插入排序等功能,你可以根据自己的需求进行修改。排序方式由用户选择,可以按照姓名的非递减字典序排序、按照学号递增排序或按照成绩的非递减排序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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://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://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://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)