通过快速排序对学生成绩进行排序,不知道为什么没有结果
程序代码:
#include<stdio.h> #include<string.h> struct Student { char num[6]; char name[10]; int math; int english; int general; }; struct Student stu[100]={{"05001","Jam",78,68,146},{"05002","Bill",80,98,178},{"05003","Bob",82,83,165},{"05004","Sam",69,82,151},{"05005","Steven",94,83,177}}; void exchange(struct Student stu[],struct Student *p1,struct Student *p2) //交换函数 { int matht,englisht,generalt; char namet[10],numt[6]; strcpy(numt,p1->num);strcpy(p1->num,p2->num);strcpy(p2->num,numt); strcpy(namet,p1->name);strcpy(p1->name,p2->name);strcpy(p2->name,namet); matht=p1->math;p1->math=p2->math;p2->math=matht; englisht=p1->english;p1->english=p2->english;p2->english=englisht; generalt=p1->general;p1->general=p2->general;p2->general=generalt; } void bubble(struct Student stu[],int l,int h) //冒泡排序函数 { int i,j; for(i=h;i<=l;i--) for(j=l;j<i;j++) if(strcmp(stu[j].num,stu[j+1].num)==1) exchange(stu,&stu[j],&stu[j+1]); } void main() //信息排序(快速排序,将数据分为较小与较大两部分,在分别进行冒泡排序) { int i,j,k; int i1,j1; int hign,midle; char key[6]; struct Student *p1,*p2; strcpy(key,stu[0].num); for(k=0;k<100;k++) if(stu[k].num[0]==NULL) { hign=k-1; break; } for(i=0,j=hign;i==j;) { if(strcmp(stu[i].num,key)==-1) { i++; i1=0; } else i1=1; if(strcmp(stu[j].num,key)==1) { j--; j1=0; } else j1=1; while(i1&&j1) { p1=&stu[i]; p2=&stu[j]; exchange(stu,p1,p2); //交换 } } midle=i;; bubble(stu,0,midle); //冒泡排序 bubble(stu,midle+1,hign); for(i=0;i<100;i++) if(stu[i].num!=NULL) printf("%s %s %d %d %d\n",stu[i].num,stu[i].name,stu[i].math,stu[i].english,stu[i].general); }