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

loading 分享 2026-7-25 下载文档

实验课题:

【用C描述课本的同学】有以下结构体构成的数组:

struct StudentInfo { char ID[10];

char * name; float score;

}StuInfo[12]= {

{\ {\ {\ {\ {\ {\ {\ {\ {\ {\ {\ {\ };

1 使用直接插入的排序方法按照学号的顺序对以上数组进行排序(递增);

2 分别用归并排序和快速排序按照姓名的顺序对以上数组进行排序(递增),有3人的名字是\,注意观察排序是否稳定。

程序代码: 第一种:

#include #include #include #include #define Cutoff (3)

struct StudentInfo { char ID[10]; char * name; double score; }StuInfo[12]= {

{\ {\ {\ {\ {\ {\ {\ {\ {\ {\ {\ {\ };

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,Tmp.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) {

MSort(A,TmpArray,0,N-1); free(TmpArray); } else {

printf(\ } }

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)


数据结构C实现排序:直接插入、归并和快速排序(递增)学号.doc 将本文的Word文档下载到电脑
搜索更多关于: 数据结构C实现排序:直接插入、归并和快速排序(递增)学号 的文档
相关推荐
相关阅读