本来是想利用快速排序实现对学习成绩的升序输出,算法时间复杂度为nlogn
#include<stdio.h>#include<string.h>#include<stdlib.h>typedef struct Node{ int num; char name[20]; int sex; char major[20]; int bj; float grade;};typedef struct{ struct Node stu[1000]; int length;}sqlist; int Partition(sqlist *L,int low,int high){ L->stu[0]=L->stu [low]; int g=L->stu[low].grade; while(low!=high) //判断条件也可以写成low<high { while(low<high&&L->stu[high].grade>=g){ high--; } L->stu[low]=L->stu[high]; while(low<high&&L->stu[low].grade<=g){ low++; } L->stu[high]=L->stu[low]; } L->stu[low]=L->stu[0]; return low;}void qsort(sqlist *L,int low,int high){ if(low<high) { int partition=Partition(L,low,high);//获取low=high时的位置 qsort(L,low,partition-1);//递归右子数组 qsort(L,partition+1,high);//递归左子数组 }}void quicksort(sqlist *L){ qsort(L,0,L->length-1);}int main(){ int n; Node stu[1000]; sqlist L=(sqlist)malloc(sizeof(sqlist)); printf("学生总人数为:"); scanf("%d",&n); printf("学号和绩点依次为:"); for(int i=0;i<n;i++){ scanf("%d %d",&L->stu[i].num ,&L->stu[i].grade); } quicksort(L); for(int i=1;i<=L->length;i++){ printf("%f\n",L->stu[i].grade); } return 0; }
devc++编译没有报错,但是运行后出来横线
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
悬赏问题
- ¥15 c程序不知道为什么得不到结果
- ¥40 复杂的限制性的商函数处理
- ¥15 程序不包含适用于入口点的静态Main方法
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置