当前位置:文档之家› 数据结构C实现排序:直接插入、归并和快速排序(递增)学号

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

实验课题:【用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<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]={{"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,)>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<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){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);i=Left;j=Right-1;for(;;){while(strcmp(A[++i].name,)<0){}while(strcmp(A[--j].name,)>0){}if(i<j){Swap(&A[i],&A[j]);Tmp=A[i];A[i]=A[j];A[j]=Tmp;}elsebreak;}Swap(&A[i],&A[Right-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;i<12;i++){printf("%s,%s,%0.1f\n",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);}printf("\n\n");Mergesort(StuInfo,12);for(i=0;i<12;i++){printf("%s,%s,%0.1f\n",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);}printf("\n\n");Quicksort(StuInfo,12);for(i=0;i<12;i++){printf("%s,%s,%0.1f\n",StuInfo[i].ID,StuInfo[i].name,StuInfo[i].score);}}#include<stdio.h>#include<stdlib.h>#include<string.h>typedef struct StudentInfo ElementType;struct StudentInfo{char ID[11];char *name;double score;}StuInfo[12]={{"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.9},{"0400830211","BOBI",87.6}};void InsertionSort(ElementType A[],int N){int j,P;ElementType 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 InsertionSort2(ElementType A[],int N){int j,P;ElementType Tmp;for(P=1;P<N;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 (ElementType A[],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(A[Lpos].name,A[Rpos].name)<=0)TmpArray[Tmpos++]=A[Lpos++];elseTmpArray[Tmpos++]=A[Rpos++];}while(Lpos<=LeftEnd)TmpArray[Tmpos++]=A[Lpos++];while(Rpos<=RightEnd)TmpArray[Tmpos++]=A[Rpos++];for(i=0;i<NumElements;i++,RightEnd--)A[RightEnd]=TmpArray[RightEnd];}void MSort(ElementType A[],ElementType 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 Mergsort(ElementType A[],int N){ElementType *TmpArray;TmpArray=(ElementType*)malloc(N*sizeof(ElementType));if(TmpArray!=NULL){MSort(A,TmpArray,0,N-1);free(TmpArray);}elseprintf("No space for tmp array!!");}void Swap(ElementType *A,ElementType *B){ElementType XX;XX=*A;*A=*B;*B=XX;}ElementType Median3(ElementType A[],int Left,int Right){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];}#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[++i].name,)<0){}while(strcmp(A[--j].name,)>0){}if(i<j)Swap(&A[i],&A[j]);elsebreak;}Swap(&A[i],&A[Right-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;i<12;i++){printf("%-15s",StuInfo[i].ID);printf("%-10s",StuInfo[i].name);printf("%.1f\n",StuInfo[i].score);}}void main(){printf("原顺序为:\n\n");PrintInfo();printf("用插入法排序为:\n\n");InsertionSort(StuInfo,12);PrintInfo();printf("用归并法排序为:\n\n");Mergsort(StuInfo,12);PrintInfo();printf("用快速法排序为:\n\n");Qsort(StuInfo,0,11);PrintInfo();}。

相关主题