数据结构C实现排序:直接插入、归并和快速排序(递增)学号
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
实验课题:
【用C描述课本的同学】有以下结构体构成的数组:
struct StudentInfo
{
{
{"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<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define Cutoff (3)
struct StudentInfo
{ char ID[10];
char * name;
double score;
}StuInfo[12]=
{ char ID[10]; char * name; float score; }StuInfo[12]=
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
{"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<N;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<N;p++)
{
Tmp = A[p];
for(j=p; j>0&&strcmp(A[j-1].name,http://www.77cn.com.cn)>0 ; j--)
{
A[j]=A[j-1];
}
A[j]=Tmp;
}
}
void Merge(struct StudentInfo A[],struct StudentInfo TmpArray[],int Lpos,int Rpos,int RightEnd) {
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
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<NumElements;i++,RightEnd--)
{
A[RightEnd]=TmpArray[RightEnd];
}
}
void MSort(struct StudentInfo A[],struct StudentInfo TmpArray[],int Left,int Right) {
int Center;
if(Left<Right)
{
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)
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
{
MSort(A,TmpArray,0,N-1);
free(TmpArray);
}
else
{
printf("No space for tmp array!!");
}
}
void Swap(struct StudentInfo A[],struct StudentInfo B[])
{
struct StudentInfo *Tmp;
Tmp=A;
A=B;
B=Tmp;
}
struct StudentInfo Median3(struct StudentInfo A[],int Left,int Right)
{
struct StudentInfo Tmp;
int Center=(Left+Right)/2;
if(strcmp(A[Left].name,A[Center].name)>0)
{
Swap(&A[Left],&A[Center]);
}
if(strcmp(A[Left].name,A[Right].name)>0)
{
Swap(&A[Left],&A[Right]);
}
if(strcmp(A[Center].name,A[Right].name)>0)
{
Swap(&A[Center],&A[Right]);
}
Swap(&A[Center],&A[Right-1]);
return A[Right-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);
1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。
i=Left;
j=Right-1;
for(;;)
{
while(strcmp(A[++i].name,http://www.77cn.com.cn)<0)
{
}
while(strcmp(A[--j].name,http://www.77cn.com …… 此处隐藏:6128字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [公文资料]市场营销专员岗位职责
- [公文资料]综合部经理岗位职责
- [公文资料]会计助理岗位职责
- [公文资料]林业站站长职责
- [公文资料]菜品研发部岗位职责
- [公文资料]街道综治办工作职责
- [公文资料]酒店前台的工作职责
- [公文资料]销售部经理岗位职责
- [公文资料]工程部副经理岗位职责
- [公文资料]手术室护士工作职责
- [公文资料]银行客户经理职责
- [公文资料]汽车4s店市场专员职责
- [公文资料]服装店长工作职责
- [公文资料]采购总监岗位职责
- [公文资料]大学行政秘书工作职责
- [公文资料]学校财务人员岗位职责
- [公文资料]财务统计员岗位职责
- [公文资料]物业工程主管工作职责
- [公文资料]公司后勤工作职责
- [公文资料]采矿工程师岗位职责
- 门面出租合同样板(门面出租的合同)
- 自用房屋租赁合同 自住房租房合同(汇总
- 最新酒店劳动合同管理制度(11篇)(酒店
- 2025年无产权车库买卖合同实用(14篇)(
- 建筑工程农民工劳动合同十五篇(通用)(
- 最新深圳标准劳动合同 深圳劳动合同如
- 解除劳动合同通知书(实用6篇)(解除劳动
- 2025年二手房屋买卖合同范围精选(二十
- 最新融资贷款居间合同大全(22篇)(融资
- 2025年个人二手房屋买卖合同协议书四篇
- 2025年果树苗木买卖合约书 签订果树苗
- 广东省劳动合同书填写(21篇)(广东省劳
- 最新餐饮行业没有劳动合同 劳动法餐饮
- 农村土地买卖合同(汇总21篇)(农村土地
- 最新房屋转租合同模版21篇(通用)(标准
- 2025年进口合同号查询五篇(大全)(进口
- 农村建房包工包料合同(通用8篇)(农村建
- 2025年安装监控合同协议书(15篇)(2025
- 2025年企业租赁经营合同(模板9篇)(2025
- 最新郊区土地租赁合同(优质23篇)(最新




