数据结构C实现排序-直接插入,归并和快速排序(递增)学号_第1页
数据结构C实现排序-直接插入,归并和快速排序(递增)学号_第2页
免费预览已结束,剩余8页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、!-实验课题:【用 C 描述课本的同学】有以下结构体构成的数组: struct StudentInfo char ID10;char * name;float score;StuInfo12=0800301105, JACK, 95,0800201505, LUN, 85,0400820115, MARY, 75.5,0400850122, KATE, 78.9,0500201011, LILI, 88,0800401105, JACK, 96,0600830105, JAN, 98.4,0952520012, SAM, 75,9721000045, OSCAR, 64,0700301105,

2、 JACK, 97,0458003312, ZOE, 68.9,0400830211, BOBI, 87.6;1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增) ;2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序 (递增), 有 3 人的名字 是JACK,注意观察排序是否稳定。程序代码 : 第一种: #include #include #include #include#define Cutoff (3)struct StudentInfo char ID10; char * name; double score;StuInfo12=0800301105, JACK

3、, 95,0800201505, LUN, 85,0400820115, MARY, 75.5,0400850122, KATE, 78.9,0500201011, LILI, 88,0800401105, JACK, 96,0600830105, JAN, 98.4,0952520012, SAM, 75,!-0721000045, OSCAR, 64,0700301105, JACK, 97,0458003312, ZOE, 68.9,0400830211, BOBI, 87.6 ,;void InsertionSort(struct StudentInfo A,int N)int j,p

4、;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 TmpArray,int Lpos,int R

5、pos,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=RightEnd)TmpArr

6、ayTmpPos+=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,ARight

7、.name)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(&am

8、p;Ai,&Aj);Tmp=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,St

9、uI,StuInfoi.score);!-printf(nn); Mergesort(StuInfo,12); for(i=0;i12;i+)printf(%s,%s,%0.1fn,StuInfoi.ID,StuI,StuInfoi.score);printf(nn);Quicksort(StuInfo,12); for(i=0;i12;i+)printf(%s,%s,%0.1fn,StuInfoi.ID,StuI,StuInfoi.score);#include#include #include typedef struct Studen

10、tInfo ElementType; struct StudentInfochar ID11;char *name;double score;StuInfo12=0800301105,JACK,95, 0800201505,LUN,85, 0400820115,MARY,75.05,0400850122,KATE,78.9, 0500201011,LILI,88, 0800401105,JACK,96,0600830105,JAN,98.4, 0952520012,SAM,75, 9721000045,OSCAR,64,0700301105,JACK,97, 0458003312,ZOE,68

11、.9, 0400830211,BOBI,87.6;void InsertionSort(ElementType A,int N)int j,P;!-ElementType 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

12、,ElementType TmpArray,int Lpos,int Rpos,int RightEnd)int i,LeftEnd,NumElements,Tmpos;LeftEnd=Rpos-1;Tmpos=Lpos;NumElements=RightEnd-Lpos+1; while(Lpos=LeftEnd&Rpos=RightEnd)if(strcmp(AL,AR)=0) TmpArrayTmpos+=ALpos+;else TmpArrayTmpos+=ARpos+;while(Lpos=LeftEnd)TmpArrayTmpos+=ALpo

13、s+; while(Rpos=RightEnd)TmpArrayTmpos+=ARpos+;for(i=0;iNumElements;i+,RightEnd-) ARightEnd=TmpArrayRightEnd;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(A

14、C,AR)0)Swap(&ACenter,&ARight);Swap(&ACenter,&ARight-1);return ARight-1;#define Cutoff 3void Qsort(ElementType 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 PrintInfo()int i;printf( 学号 ID 姓名 分数 n); for(i=0;i12;i+)printf(%-15s,StuInfoi.ID);printf(%-10s,StuI);p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论