数据结构C语言版之排序(各种排序)
//排序: /* 1、录入学生基本信息 2、直接插入排序 3、冒泡排序 4、快速排序 5、简单选择排序 6、堆排序 7、2-路归并排序 8、输出学生信息 */ #include<stdio.h> #include<stdlib.h> typedef struct{ char name[20]; int num; int score; int degree; }Student,*student; int save(student w,int n) { FILE *fp; if((fp=fopen("stu.txt","wb"))==NULL) { printf("cannot creat the file!\n"); return 1; } for(int i=1;i<=n;i++) { fwrite(w+1,sizeof(Student),1,fp); w++; } fclose(fp); return 0; } void Studentinfo(student stu,int n) { int i; printf("录入学生的信息:\n"); printf("姓名 学号 分数:\n"); for(i=1;i<=n;i++) { scanf("%s",stu[i].name); scanf("%d",&stu[i].num); scanf("%d",&stu[i].score); getchar(); } save(stu,n); } int openfile(student p,int n) { FILE *fp; if((fp=fopen("stu.txt","rb"))==NULL) { printf("cannot open the file!\n"); return 1; } for(int i=1;i<=n;i++) { fread(p+1,sizeof(Student),1,fp); p++; } fclose(fp); return 0; } void showinfo(student stu,int n) { int i; printf("进行排序后学生的信息:\n"); printf("姓名 学号 分数 名次:\n"); for(i=1;i<=n;i++) { stu[i].degree=n-i+1; printf("%s ",stu[i].name); printf("%5d%5d%5d\n",stu[i].num,stu[i].score,stu[i].degree); } } void InsertSort(student stu,int n) { int i,j; for(i=2;i<=n;i++) { if(stu[i].score<stu[i-1].score) { stu[0]=stu[i]; stu[i]=stu[i-1]; for(j=i-2;stu[0].score<stu[j].score;j--) stu[j]=stu[j-1]; stu[j+1]=stu[0]; } } showinfo(stu,n); } void BubbleSort(student stu,int n) { int i,j; int flag=1; Student t; for(i=1;i<=n&&flag;i++) { flag=0; for(j=1;j<=n-i;j++) { if(stu[j].score>stu[j+1].score) { flag=1; t=stu[j]; stu[j]=stu[j+1]; stu[j+1]=t; } } } showinfo(stu,n); } int Partition(student &stu,int low,int high) { stu[0]=stu[low]; int keyword=stu[low].score; while(low<high){ while(low<high&&stu[high].score>=keyword) high--; stu[low]=stu[high]; while(low<high&&stu[low].score<=keyword) low++; stu[high]=stu[low]; } stu[low]=stu[0]; return low; } void QSort(student &stu,int low,int high) { int temp; if(low<high) { temp=Partition(stu,low,high); QSort(stu,low,temp-1); QSort(stu,temp+1,high); } } void SlectsSort(student stu,int n) { int i,j,k; Student temp; for(i=1;i<n;i++) { k=i; for(j=i+1;j<=n;j++) if(stu[k].score>stu[j].score) k=j; if(k!=i) { temp=stu[i]; stu[i]=stu[k]; stu[k]=temp; } } showinfo(stu,n); } void HeapAdjust(student stu,int s,int n) { Student rc; rc=stu[s]; for(int j=2*s;j<=n;j*=2) { if(j<n&&stu[j].score<stu[j+1].score) j++; if(rc.score>stu[j].score) break; stu[s]=rc; } } void HSort(student stu,int n) {//堆排序 int i; Student t; for(i=n/2;i>0;i--) HeapAdjust(stu,i,n); for(i=n;i>1;i--) { t=stu[1]; stu[1]=stu[i]; stu[i]=t; HeapAdjust(stu,1,i-1); } } student S; void merge(Student stu[],int i,int m,int n) { int j,k; j=m+1; k=i; while(j<=n&&i<=m) { if(stu[i].score<=stu[j].score) S[k++]=stu[i++]; else S[k++]=stu[j++]; } while(j<=n) S[k++]=stu[j++]; while(i<=m) S[k++]=stu[i++]; for(int t=i;t<=n;t++) stu[t]=S[t]; } void MergeSort(student stu,int low,int high) { if(low==high) return; int m; m=(low+high)/2; MergeSort(stu,low,m); MergeSort(stu,m+1,high); merge(stu,low,m,high); } int main() { student stu; int n,low,high; printf("请输入学生个数:\n"); scanf("%d",&n); low=1;high=n; stu=(student)malloc((n+1)*sizeof(Student)); Studentinfo(stu,n); openfile(stu,n); InsertSort(stu,n); openfile(stu,n); BubbleSort(stu,n); openfile(stu,n); QSort(stu,low,high); showinfo(stu,n); openfile(stu,n); SlectsSort(stu,n); HSort(stu,n); showinfo(stu,n); /*MergeSort(stu,low,high); showinfo(stu,n);*/ return 0; }
不为失败找原因,要为成功找方法!