数据结构C实现排序:直接插入、归并和快速排序(递增)学号
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验课题:
【用C描述课本的同学】有以下结构体构成的数组:
struct StudentInfo
{ char ID[10];
char * name;
float score;
}StuInfo[12]=
{
{"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", "JACK", 97},
{"0458003312", "ZOE", 68.9},
{"0400830211", "BOBI", 87.6}
};
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);
2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
程序代码:
第一种:
#include
#include
#include
#include
#define Cutoff (3)
struct StudentInfo
{ char ID[10];
char * name;
double score;
}StuInfo[12]=
{
{"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},
{"0721000045", "OSCAR", 64},
{"0700301105", "JACK", 97},
{"0458003312", "ZOE", 68.9},
{"0400830211", "BOBI", 87.6} ,
};
void InsertionSort(struct StudentInfo A[],int N)
{
int j,p;
struct StudentInfo Tmp;
for(p=1;p { Tmp = A[p]; for(j=p; j>0&&strcmp(A[j-1].ID,Tmp.ID)>0 ; j--) { A[j]=A[j-1]; } A[j]=Tmp; } } void InsertionSort1(struct StudentInfo A[],int N) { int j,p; struct StudentInfo Tmp; for(p=1;p { Tmp = A[p]; for(j=p; j>0&&strcmp(A[j-1].name,)>0 ; j--) { A[j]=A[j-1]; } A[j]=Tmp; } } void Merge(struct StudentInfo A[],struct StudentInfo 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(A[Lpos].name,A[Rpos].name)<=0) { TmpArray[TmpPos++]=A[Lpos++]; } else { TmpArray[TmpPos++]=A[Rpos++]; } } while(Lpos<=LeftEnd) { TmpArray[TmpPos++]=A[Lpos++]; } while(Rpos<=RightEnd) { TmpArray[TmpPos++]=A[Rpos++]; } for(i=0;i { A[RightEnd]=TmpArray[RightEnd]; } } void MSort(struct StudentInfo A[],struct StudentInfo TmpArray[],int Left,int Right) { int Center; if(Left { Center=(Left+Right)/2; MSort(A,TmpArray,Left,Center); MSort(A,TmpArray,Center+1,Right); Merge(A,TmpArray,Left,Center+1,Right); } } void Mergesort(struct StudentInfo A[],int N) { struct StudentInfo *TmpArray; TmpArray=malloc(N*sizeof(struct StudentInfo)); if(TmpArray !=NULL)