数据结构C语言版之排序(各种排序)
来源:互联网 发布:地图 数据可视化工具 编辑:程序博客网 时间:2024/05/29 10:49
//排序:/* 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;}
0 0
- 数据结构C语言版之排序(各种排序)
- 数据结构之简单排序(C语言版)
- 数据结构c语言版之直接插入排序
- 数据结构c语言版之简单选择排序
- 数据结构之插入排序的实现(C语言版)
- 各种排序算法 C语言版
- 冒泡排序之C语言版
- C语言版各种排序算法(持续更新中)
- 数据结构(C语言版)规范代码之图(邻接表的拓扑排序)
- 数据结构与算法(C语言版)__冒泡排序
- 数据结构与算法(C语言版)__选择排序
- 数据结构与算法(C语言版)__插入排序
- 数据结构与算法(C语言版)__快速排序
- 数据结构与算法(C语言版)__归并排序
- 数据结构与算法(C语言版)__堆排序
- 数据结构(c语言版)学习笔记——排序算法
- 数据结构(c语言版)学习笔记——排序算法
- 深入浅出数据结构C语言版(16)——插入排序
- CSS3伪类选择器
- C语言---基础
- 数据结构C语言版之查找(折半选择快速等)
- nginx浅析--1
- windows 7系统封装初级教程
- 数据结构C语言版之排序(各种排序)
- 递归应用----指向整型数据的指针变量对输入的3个整数按从小到大排序
- yum搭建lnmp环境(CentOS6.5)
- 将二叉树按中序遍历转换成双向链表
- gedit写Makefile出现Makefile:16: *** missing separator. Stop
- ImportError: No module named MySQLdb 解决方案
- 答与微博前端教主在吃饭时讨论到的一道"微软面试题"
- CSS选择器
- 用Redis存储Tomcat集群的Session