教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 范文大全 > 公文资料 >

数据结构C实现排序:直接插入、归并和快速排序(递增)学号

来源:网络收集 时间:2026-08-28
导读: 1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是"JACK",注意观察排序是否稳定。 实验课题: 【用C描述课本的同学】有以下结构体构成的数组: struct Stud

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字,全部文档内容请下载后查看。喜欢就下载吧 ……

数据结构C实现排序:直接插入、归并和快速排序(递增)学号.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/fanwen/709865.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)