




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验课题一:【用C描述课本的同学】有以下结构体构成的数组:struct StudentInfochar ID10;char * name;float score;StuInfo12=, JACK, 95, LUN, 85, MARY, 75.5, KATE, 78.9, LILI, 88, JACK, 96, JAN, 98.4, SAM, 75, OSCAR, 64, JACK, 97, ZOE, 68.9, BOBI, 87.6 ; 1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是JAC
2、K,注意观察排序是否稳定。程序1#include#include#includetypedef struct StudentInfo ElementType;struct StudentInfochar ID11;char *name;double score;StuInfo12=,JACK,95,LUN,85,MARY,75.05,KATE,78.9,LILI,88,JACK,96,JAN,98.4,SAM,75,OSCAR,64,JACK,97,ZOE,68.9,BOBI,87.6;void InsertionSort(ElementType A,int N)int j,P;Element
3、Type Tmp;for(P=1;P0&strcmp(Aj-1.ID,Tmp.ID)0;j-)Aj=Aj-1;Aj=Tmp;void InsertionSort2(ElementType A,int N)int j,P;ElementType Tmp;for(P=1;P0&strcmp(A,T)0;j-)Aj=Aj-1;Aj=Tmp;void Merge (ElementType A,ElementType TmpArray,int Lpos,int Rpos,int RightEnd)int i,LeftEnd,NumElements,Tmpos;LeftEnd
4、=Rpos-1;Tmpos=Lpos;NumElements=RightEnd-Lpos+1;while(Lpos=LeftEnd&Rpos=RightEnd)if(strcmp(AL,AR)=0)TmpArrayTmpos+=ALpos+;elseTmpArrayTmpos+=ARpos+;while(Lpos=LeftEnd)TmpArrayTmpos+=ALpos+;while(Rpos=RightEnd)TmpArrayTmpos+=ARpos+;for(i=0;iNumElements;i+,RightEnd-)ARightEnd=TmpArrayRi
5、ghtEnd;void MSort(ElementType A,ElementType TmpArray,int Left,int Right)int center;if(Left0)Swap(&ALeft,&ACenter);if(strcmp(AL,AR)0)Swap(&ALeft,&ARight);if(strcmp(AC,AR)0)Swap(&ACenter,&ARight);Swap(&ACenter,&ARight-1);return ARight-1;#define Cutoff 3void Qsort(El
6、ementType A,int Left,int Right)int i,j;ElementType Pivot;if(Left+Cutoff=Right)Pivot=Median3(A,Left,Right);i=Left;j=Right-1;for(;)while(strcmp(A+,P)0)if(ij)Swap(&Ai,&Aj);elsebreak;Swap(&Ai,&ARight-1);Qsort(A,Left,i-1);Qsort(A,i+1,Right);elseInsertionSort2(A+Left,Right-Left+1);void Prin
7、tInfo()int i;printf( 学号ID 姓名 分数n);for(i=0;i12;i+)printf(%-15s,StuInfoi.ID);printf(%-10s,StuI);printf(%.1fn,StuInfoi.score);void main()printf(原顺序为:nn);PrintInfo();printf(用插入法排序为:nn);InsertionSort(StuInfo,12);PrintInfo();printf(用归并法排序为:nn);Mergsort(StuInfo,12);PrintInfo();printf(用快速法排序为:nn);Q
8、sort(StuInfo,0,11);PrintInfo();程序2#include#include#include#include#define Cutoff (3)struct StudentInfochar ID10;char * name;double score;StuInfo12=, JACK, 95, LUN, 85, MARY, 75.5, KATE, 78.9, LILI, 88, JACK, 96, JAN, 98.4, SAM, 75, OSCAR, 64, JACK, 97, ZOE, 68.9, BOBI, 87.6 ,; void InsertionSort(str
9、uct StudentInfo A,int N)int j,p;struct StudentInfo Tmp; for(p=1;p0&strcmp(Aj-1.ID,Tmp.ID)0 ; j-)Aj=Aj-1;Aj=Tmp;void InsertionSort1(struct StudentInfo A,int N)int j,p;struct StudentInfo Tmp; for(p=1;p0&strcmp(A,T)0 ; j-)Aj=Aj-1;Aj=Tmp;void Merge(struct StudentInfo A,struct StudentInfo
10、TmpArray,int Lpos,int Rpos,int RightEnd)int i,LeftEnd,NumElements,TmpPos;LeftEnd=Rpos-1;TmpPos=Lpos;NumElements=RightEnd-Lpos+1;while(Lpos=LeftEnd & Rpos=RightEnd)if(strcmp(AL,AR)=0)TmpArrayTmpPos+=ALpos+;elseTmpArrayTmpPos+=ARpos+;while(Lpos=LeftEnd)TmpArrayTmpPos+=ALpos+;while(Rpos
11、=RightEnd)TmpArrayTmpPos+=ARpos+; for(i=0;iNumElements;i+,RightEnd-)ARightEnd=TmpArrayRightEnd;void MSort(struct StudentInfo A,struct StudentInfo TmpArray,int Left,int Right)int Center;if(Left0) Swap(&ALeft,&ACenter); if(strcmp(AL,AR)0) Swap(&ALeft,&ARight); if(strcmp(AC,A
12、R)0) Swap(&ACenter,&ARight); Swap(&ACenter,&ARight-1); return ARight-1;void Qsort(struct StudentInfo A,int Left,int Right)int i,j; struct StudentInfo Pivot,Tmp;if(Left+Cutoff=Right)Pivot=Median3(A,Left,Right);i=Left;j=Right-1;for(;)while(strcmp(A+,P)0)if(ij) Swap(&Ai,&Aj); Tm
13、p=Ai;Ai=Aj;Aj=Tmp;elsebreak;Swap(&Ai,&ARight-1); Qsort(A,Left,i-1);Qsort(A,i+1,Right); InsertionSort1(A+Left,Right-Left+1);void Quicksort(struct StudentInfo A,int N)Qsort(A,0,N-1);void main()int i=0;InsertionSort(StuInfo,12); for(i=0;i12;i+)printf(%s,%s,%0.1fn,StuInfoi.ID,StuI,StuInfoi.score);printf(nn); Merg
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025关于版权行业规范合同
- 2025租赁合同范本简化版
- 2025粳米买卖合同
- 2025年店面租赁合同范本下载
- 胃癌化疗护理常规
- 普通心理学(第2版)课件 第三章 感觉
- 草原生字组词课件
- 义务教育质量监测结果应用主题教育研讨活动英语心得体会模版
- 高中圆锥曲线(椭圆、双曲线、抛物线)规律技巧总结模版
- 2025年HR年度个人工作总结模版
- 人教版七年级上生命的思考珍视生命微课
- 数学手册(高清版)
- 《安井食品采购成本管理问题研究【开题报告+文献综述+正文】》17000字
- 义务教育语文课程标准(2022)测试题带答案(20套)
- 招聘与配置课程心得体会5篇
- 东芝电梯紧急救出操作指南
- 保护性约束完整版
- 外贸发票 PI 形式发票模板范例
- 2022年新人教版高中生物选择性必修三答案与提示
- 中职护理专业课程标准
- GB/T 6742-2007色漆和清漆弯曲试验(圆柱轴)
评论
0/150
提交评论