内部排序算法的性能分析.docx
《内部排序算法的性能分析.docx》由会员分享,可在线阅读,更多相关《内部排序算法的性能分析.docx(17页珍藏版)》请在冰豆网上搜索。
内部排序算法的性能分析
#include
#defineM50
#defineMAX100000;
typedefstruct
{
intweight;//结点权值
intparent,lchild,rchild;
}HTNODE,*HUFFMANTREE;
typedefchar**HUFFMANCODE;//动态分配数组存储哈夫曼编码表
intkey;/*关键字*/
}RecordNode;/*排序节点的类型*/
RecordNode*record;
intn;/*排序对象的大小*/
}SortObject;//待排序序列
HUFFMANTREEhuffmantree(intn,intweight[])//构建哈夫曼树
intm1,m2,k;
inti,j,x1,x2;
HUFFMANTREEht;
ht=(HUFFMANTREE)malloc((2*n)*sizeof(HTNODE));
for(i=1;i<(2*n);i++)//初始化哈夫曼树中各结点的数据,没初始值的赋值为0
ht[i].parent=ht[i].lchild=ht[i].rchild=0;
if(i<=n)
ht[i].weight=weight[i];
else
ht[i].weight=0;
}
for(i=1;i{m1=m2=MAX;x1=x2=0;for(j=1;j<(n+i);j++){if((ht[j].weight{m2=m1;x2=x1;m1=ht[j].weight;x1=j;}elseif((ht[j].weight{m2=ht[j].weight;x2=j;}}k=n+i;ht[x1].parent=ht[x2].parent=k;ht[k].weight=m1+m2;ht[k].lchild=x1;ht[k].rchild=x2;}returnht;}voidhuffmancoding(intn,HUFFMANCODEhc,HUFFMANTREEht,charstr[]){inti,start,child,father;char*cd;hc=(HUFFMANCODE)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间cd[n-1]='\0';//编码结束符for(i=1;i<=n;++i)//逐个字符求哈夫曼编码{start=n-1;for(child=i,father=ht[i].parent;father!=0;child=father,father=ht[father].parent)/*从叶子结点到根结点求逆向编码*/if(ht[father].lchild==child)cd[--start]='0';elsecd[--start]='1';hc[i]=(char*)malloc((n-start)*sizeof(char));//为i个字符编码分配空间strcpy(hc[i],&cd[start]);//从cd复制哈夫曼编码串到hc}free(cd);//释放工作空间for(i=1;i<=n;++i){printf("\n%c的编码:",str[i]);printf("%s\n",hc[i]);}}voidhuffman(){inti,j,k,m,n;charstr[50];intweight[50];HUFFMANCODEhc=NULL;HUFFMANTREEht;fflush(stdin); printf("\n请输入字符(一次性连续输入所求的字符):");/*如:abcjhjg不要输成abcjhig,即字符间不加空格*/gets(str);for(j=0;j<50;j++){if(str[j]=='\0')break;}n=j;for(j=n;j>0;j--)str[j]=str[j-1];str[n+1]='\0';for(k=0;k{printf("\n请输入%c的权值:",str[k+1]);scanf("%d",&weight[k]);}for(k=n;k>0;k--)weight[k]=weight[k-1];weight[0]=0;ht=huffmantree(n,weight);huffmancoding(n,hc,ht,str);}voidInsertSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;fflush(stdin);if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=1;in;i++){temp=pvector->record[i];(*exchange)++;j=i-1;while((temp.keyrecord[j].key)&&(j>=0)){(*compare)++;(*exchange)++;pvector->record[j+1]=pvector->record[j];j--;}if(j!=(i-1)){pvector->record[j+1]=temp;(*exchange)++;}}free(pvector);}voidSelectSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){k=i;for(j=i+1;jn;j++){(*compare)++;if(pvector->record[j].keyrecord[k].key)k=j;}if(k!=i){temp=pvector->record[i];pvector->record[i]=pvector->record[k];pvector->record[k]=temp;(*exchange)+=3;}}free(pvector);}voidBubbleSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,noswap,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){noswap=1;for(j=0;jn-i-1;j++){(*compare)++;if(pvector->record[j+1].keyrecord[j].key){temp=pvector->record[j];pvector->record[j]=pvector->record[j+1];pvector->record[j+1]=temp;(*exchange)+=3;noswap=0;}}if(noswap)break;}free(pvector);}voidShellSort(SortObject*p,intd,unsignedlong*compare,unsignedlong*exchange){inti,j,increment,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(increment=d;increment>0;increment/=2){for(i=increment;in;i++){temp=pvector->record[i];(*exchange)++;j=i-increment;while(j>=0&&temp.keyrecord[j].key){(*compare)++;pvector->record[j+increment]=pvector->record[j];(*exchange)++;j-=increment;}pvector->record[j+increment]=temp;(*exchange)++;}}free(pvector);}voidQuickSort(SortObject*pvector,intleft,intright,unsignedlong*compare,unsignedlong*exchange){inti,j;RecordNodetemp;if(left>=right)return;i=left;j=right;temp=pvector->record[i];(*exchange)++;while(i!=j){while((pvector->record[j].key>=temp.key)&&(j>i)){(*compare)++;j--;}if(i{pvector->record[i++]=pvector->record[j];(*exchange)++;}while((pvector->record[i].key<=temp.key)&&(j>i)){(*compare)++;i++;}if(i{pvector->record[j--]=pvector->record[i];(*exchange)++;}}pvector->record[i]=temp;(*exchange)++;QuickSort(pvector,left,i-1,compare,exchange);QuickSort(pvector,i+1,right,compare,exchange);}voidSortMethod(void){inti,j,k,l;unsignedlongnum[5][10]={0};unsignedlongsum[10]={0};SortObject*pvector;fflush(stdin);printf("请输入待排序的随机数个数:\n");scanf("%d",&k);pvector=(SortObject*)malloc(sizeof(SortObject));for(j=0;j<5;j++){pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
m1=m2=MAX;
x1=x2=0;
for(j=1;j<(n+i);j++)
if((ht[j].weight{m2=m1;x2=x1;m1=ht[j].weight;x1=j;}elseif((ht[j].weight{m2=ht[j].weight;x2=j;}}k=n+i;ht[x1].parent=ht[x2].parent=k;ht[k].weight=m1+m2;ht[k].lchild=x1;ht[k].rchild=x2;}returnht;}voidhuffmancoding(intn,HUFFMANCODEhc,HUFFMANTREEht,charstr[]){inti,start,child,father;char*cd;hc=(HUFFMANCODE)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间cd[n-1]='\0';//编码结束符for(i=1;i<=n;++i)//逐个字符求哈夫曼编码{start=n-1;for(child=i,father=ht[i].parent;father!=0;child=father,father=ht[father].parent)/*从叶子结点到根结点求逆向编码*/if(ht[father].lchild==child)cd[--start]='0';elsecd[--start]='1';hc[i]=(char*)malloc((n-start)*sizeof(char));//为i个字符编码分配空间strcpy(hc[i],&cd[start]);//从cd复制哈夫曼编码串到hc}free(cd);//释放工作空间for(i=1;i<=n;++i){printf("\n%c的编码:",str[i]);printf("%s\n",hc[i]);}}voidhuffman(){inti,j,k,m,n;charstr[50];intweight[50];HUFFMANCODEhc=NULL;HUFFMANTREEht;fflush(stdin); printf("\n请输入字符(一次性连续输入所求的字符):");/*如:abcjhjg不要输成abcjhig,即字符间不加空格*/gets(str);for(j=0;j<50;j++){if(str[j]=='\0')break;}n=j;for(j=n;j>0;j--)str[j]=str[j-1];str[n+1]='\0';for(k=0;k{printf("\n请输入%c的权值:",str[k+1]);scanf("%d",&weight[k]);}for(k=n;k>0;k--)weight[k]=weight[k-1];weight[0]=0;ht=huffmantree(n,weight);huffmancoding(n,hc,ht,str);}voidInsertSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;fflush(stdin);if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=1;in;i++){temp=pvector->record[i];(*exchange)++;j=i-1;while((temp.keyrecord[j].key)&&(j>=0)){(*compare)++;(*exchange)++;pvector->record[j+1]=pvector->record[j];j--;}if(j!=(i-1)){pvector->record[j+1]=temp;(*exchange)++;}}free(pvector);}voidSelectSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){k=i;for(j=i+1;jn;j++){(*compare)++;if(pvector->record[j].keyrecord[k].key)k=j;}if(k!=i){temp=pvector->record[i];pvector->record[i]=pvector->record[k];pvector->record[k]=temp;(*exchange)+=3;}}free(pvector);}voidBubbleSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,noswap,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){noswap=1;for(j=0;jn-i-1;j++){(*compare)++;if(pvector->record[j+1].keyrecord[j].key){temp=pvector->record[j];pvector->record[j]=pvector->record[j+1];pvector->record[j+1]=temp;(*exchange)+=3;noswap=0;}}if(noswap)break;}free(pvector);}voidShellSort(SortObject*p,intd,unsignedlong*compare,unsignedlong*exchange){inti,j,increment,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(increment=d;increment>0;increment/=2){for(i=increment;in;i++){temp=pvector->record[i];(*exchange)++;j=i-increment;while(j>=0&&temp.keyrecord[j].key){(*compare)++;pvector->record[j+increment]=pvector->record[j];(*exchange)++;j-=increment;}pvector->record[j+increment]=temp;(*exchange)++;}}free(pvector);}voidQuickSort(SortObject*pvector,intleft,intright,unsignedlong*compare,unsignedlong*exchange){inti,j;RecordNodetemp;if(left>=right)return;i=left;j=right;temp=pvector->record[i];(*exchange)++;while(i!=j){while((pvector->record[j].key>=temp.key)&&(j>i)){(*compare)++;j--;}if(i{pvector->record[i++]=pvector->record[j];(*exchange)++;}while((pvector->record[i].key<=temp.key)&&(j>i)){(*compare)++;i++;}if(i{pvector->record[j--]=pvector->record[i];(*exchange)++;}}pvector->record[i]=temp;(*exchange)++;QuickSort(pvector,left,i-1,compare,exchange);QuickSort(pvector,i+1,right,compare,exchange);}voidSortMethod(void){inti,j,k,l;unsignedlongnum[5][10]={0};unsignedlongsum[10]={0};SortObject*pvector;fflush(stdin);printf("请输入待排序的随机数个数:\n");scanf("%d",&k);pvector=(SortObject*)malloc(sizeof(SortObject));for(j=0;j<5;j++){pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
m2=m1;
x2=x1;
m1=ht[j].weight;
x1=j;
elseif((ht[j].weight{m2=ht[j].weight;x2=j;}}k=n+i;ht[x1].parent=ht[x2].parent=k;ht[k].weight=m1+m2;ht[k].lchild=x1;ht[k].rchild=x2;}returnht;}voidhuffmancoding(intn,HUFFMANCODEhc,HUFFMANTREEht,charstr[]){inti,start,child,father;char*cd;hc=(HUFFMANCODE)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间cd[n-1]='\0';//编码结束符for(i=1;i<=n;++i)//逐个字符求哈夫曼编码{start=n-1;for(child=i,father=ht[i].parent;father!=0;child=father,father=ht[father].parent)/*从叶子结点到根结点求逆向编码*/if(ht[father].lchild==child)cd[--start]='0';elsecd[--start]='1';hc[i]=(char*)malloc((n-start)*sizeof(char));//为i个字符编码分配空间strcpy(hc[i],&cd[start]);//从cd复制哈夫曼编码串到hc}free(cd);//释放工作空间for(i=1;i<=n;++i){printf("\n%c的编码:",str[i]);printf("%s\n",hc[i]);}}voidhuffman(){inti,j,k,m,n;charstr[50];intweight[50];HUFFMANCODEhc=NULL;HUFFMANTREEht;fflush(stdin); printf("\n请输入字符(一次性连续输入所求的字符):");/*如:abcjhjg不要输成abcjhig,即字符间不加空格*/gets(str);for(j=0;j<50;j++){if(str[j]=='\0')break;}n=j;for(j=n;j>0;j--)str[j]=str[j-1];str[n+1]='\0';for(k=0;k{printf("\n请输入%c的权值:",str[k+1]);scanf("%d",&weight[k]);}for(k=n;k>0;k--)weight[k]=weight[k-1];weight[0]=0;ht=huffmantree(n,weight);huffmancoding(n,hc,ht,str);}voidInsertSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;fflush(stdin);if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=1;in;i++){temp=pvector->record[i];(*exchange)++;j=i-1;while((temp.keyrecord[j].key)&&(j>=0)){(*compare)++;(*exchange)++;pvector->record[j+1]=pvector->record[j];j--;}if(j!=(i-1)){pvector->record[j+1]=temp;(*exchange)++;}}free(pvector);}voidSelectSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){k=i;for(j=i+1;jn;j++){(*compare)++;if(pvector->record[j].keyrecord[k].key)k=j;}if(k!=i){temp=pvector->record[i];pvector->record[i]=pvector->record[k];pvector->record[k]=temp;(*exchange)+=3;}}free(pvector);}voidBubbleSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,noswap,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){noswap=1;for(j=0;jn-i-1;j++){(*compare)++;if(pvector->record[j+1].keyrecord[j].key){temp=pvector->record[j];pvector->record[j]=pvector->record[j+1];pvector->record[j+1]=temp;(*exchange)+=3;noswap=0;}}if(noswap)break;}free(pvector);}voidShellSort(SortObject*p,intd,unsignedlong*compare,unsignedlong*exchange){inti,j,increment,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(increment=d;increment>0;increment/=2){for(i=increment;in;i++){temp=pvector->record[i];(*exchange)++;j=i-increment;while(j>=0&&temp.keyrecord[j].key){(*compare)++;pvector->record[j+increment]=pvector->record[j];(*exchange)++;j-=increment;}pvector->record[j+increment]=temp;(*exchange)++;}}free(pvector);}voidQuickSort(SortObject*pvector,intleft,intright,unsignedlong*compare,unsignedlong*exchange){inti,j;RecordNodetemp;if(left>=right)return;i=left;j=right;temp=pvector->record[i];(*exchange)++;while(i!=j){while((pvector->record[j].key>=temp.key)&&(j>i)){(*compare)++;j--;}if(i{pvector->record[i++]=pvector->record[j];(*exchange)++;}while((pvector->record[i].key<=temp.key)&&(j>i)){(*compare)++;i++;}if(i{pvector->record[j--]=pvector->record[i];(*exchange)++;}}pvector->record[i]=temp;(*exchange)++;QuickSort(pvector,left,i-1,compare,exchange);QuickSort(pvector,i+1,right,compare,exchange);}voidSortMethod(void){inti,j,k,l;unsignedlongnum[5][10]={0};unsignedlongsum[10]={0};SortObject*pvector;fflush(stdin);printf("请输入待排序的随机数个数:\n");scanf("%d",&k);pvector=(SortObject*)malloc(sizeof(SortObject));for(j=0;j<5;j++){pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
m2=ht[j].weight;
x2=j;
k=n+i;
ht[x1].parent=ht[x2].parent=k;
ht[k].weight=m1+m2;
ht[k].lchild=x1;
ht[k].rchild=x2;
returnht;
voidhuffmancoding(intn,HUFFMANCODEhc,HUFFMANTREEht,charstr[])
inti,start,child,father;
char*cd;
hc=(HUFFMANCODE)malloc((n+1)*sizeof(char*));//分配n个字符编码的头指针
cd=(char*)malloc(n*sizeof(char));//分配求编码的工作空间
cd[n-1]='\0';//编码结束符
for(i=1;i<=n;++i)//逐个字符求哈夫曼编码
start=n-1;
for(child=i,father=ht[i].parent;father!
=0;child=father,father=ht[father].parent)/*从叶子结点到根结点求逆向编码*/
if(ht[father].lchild==child)
cd[--start]='0';
cd[--start]='1';
hc[i]=(char*)malloc((n-start)*sizeof(char));//为i个字符编码分配空间
strcpy(hc[i],&cd[start]);//从cd复制哈夫曼编码串到hc
free(cd);//释放工作空间
for(i=1;i<=n;++i)
printf("\n%c的编码:
",str[i]);
printf("%s\n",hc[i]);
voidhuffman()
inti,j,k,m,n;
charstr[50];
intweight[50];
HUFFMANCODEhc=NULL;
fflush(stdin);
printf("\n请输入字符(一次性连续输入所求的字符):
");/*如:
abcjhjg不要输成abcjhig,即字符间不加空格*/
gets(str);
for(j=0;j<50;j++)
if(str[j]=='\0')
break;
n=j;
for(j=n;j>0;j--)
str[j]=str[j-1];
str[n+1]='\0';
for(k=0;k{printf("\n请输入%c的权值:",str[k+1]);scanf("%d",&weight[k]);}for(k=n;k>0;k--)weight[k]=weight[k-1];weight[0]=0;ht=huffmantree(n,weight);huffmancoding(n,hc,ht,str);}voidInsertSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;fflush(stdin);if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=1;in;i++){temp=pvector->record[i];(*exchange)++;j=i-1;while((temp.keyrecord[j].key)&&(j>=0)){(*compare)++;(*exchange)++;pvector->record[j+1]=pvector->record[j];j--;}if(j!=(i-1)){pvector->record[j+1]=temp;(*exchange)++;}}free(pvector);}voidSelectSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){k=i;for(j=i+1;jn;j++){(*compare)++;if(pvector->record[j].keyrecord[k].key)k=j;}if(k!=i){temp=pvector->record[i];pvector->record[i]=pvector->record[k];pvector->record[k]=temp;(*exchange)+=3;}}free(pvector);}voidBubbleSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange){inti,j,noswap,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(i=0;in-1;i++){noswap=1;for(j=0;jn-i-1;j++){(*compare)++;if(pvector->record[j+1].keyrecord[j].key){temp=pvector->record[j];pvector->record[j]=pvector->record[j+1];pvector->record[j+1]=temp;(*exchange)+=3;noswap=0;}}if(noswap)break;}free(pvector);}voidShellSort(SortObject*p,intd,unsignedlong*compare,unsignedlong*exchange){inti,j,increment,k;RecordNodetemp;SortObject*pvector;if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL){printf("OverFollow!");getchar();exit(1);}k=pvector->n;pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;in;i++)/*复制数组*/pvector->record[i]=p->record[i];pvector->n=p->n;*compare=0;*exchange=0;for(increment=d;increment>0;increment/=2){for(i=increment;in;i++){temp=pvector->record[i];(*exchange)++;j=i-increment;while(j>=0&&temp.keyrecord[j].key){(*compare)++;pvector->record[j+increment]=pvector->record[j];(*exchange)++;j-=increment;}pvector->record[j+increment]=temp;(*exchange)++;}}free(pvector);}voidQuickSort(SortObject*pvector,intleft,intright,unsignedlong*compare,unsignedlong*exchange){inti,j;RecordNodetemp;if(left>=right)return;i=left;j=right;temp=pvector->record[i];(*exchange)++;while(i!=j){while((pvector->record[j].key>=temp.key)&&(j>i)){(*compare)++;j--;}if(i{pvector->record[i++]=pvector->record[j];(*exchange)++;}while((pvector->record[i].key<=temp.key)&&(j>i)){(*compare)++;i++;}if(i{pvector->record[j--]=pvector->record[i];(*exchange)++;}}pvector->record[i]=temp;(*exchange)++;QuickSort(pvector,left,i-1,compare,exchange);QuickSort(pvector,i+1,right,compare,exchange);}voidSortMethod(void){inti,j,k,l;unsignedlongnum[5][10]={0};unsignedlongsum[10]={0};SortObject*pvector;fflush(stdin);printf("请输入待排序的随机数个数:\n");scanf("%d",&k);pvector=(SortObject*)malloc(sizeof(SortObject));for(j=0;j<5;j++){pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
printf("\n请输入%c的权值:
",str[k+1]);
scanf("%d",&weight[k]);
for(k=n;k>0;k--)
weight[k]=weight[k-1];
weight[0]=0;
ht=huffmantree(n,weight);
huffmancoding(n,hc,ht,str);
voidInsertSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange)
inti,j,k;
RecordNodetemp;
SortObject*pvector;
if((pvector=(SortObject*)malloc(sizeof(SortObject)))==NULL)
printf("OverFollow!
");
getchar();
exit
(1);
k=pvector->n;
pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);
for(i=0;in;i++)/*复制数组*/
pvector->record[i]=p->record[i];
pvector->n=p->n;
*compare=0;
*exchange=0;
for(i=1;in;i++)
temp=pvector->record[i];
(*exchange)++;
j=i-1;
while((temp.keyrecord[j].key)&&(j>=0))
(*compare)++;
pvector->record[j+1]=pvector->record[j];
j--;
if(j!
=(i-1))
pvector->record[j+1]=temp;
free(pvector);
voidSelectSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange)
for(i=0;in-1;i++)
k=i;
for(j=i+1;jn;j++)
if(pvector->record[j].keyrecord[k].key)
k=j;
if(k!
=i)
pvector->record[i]=pvector->record[k];
pvector->record[k]=temp;
(*exchange)+=3;
voidBubbleSort(SortObject*p,unsignedlong*compare,unsignedlong*exchange)
inti,j,noswap,k;
noswap=1;
for(j=0;jn-i-1;j++)
if(pvector->record[j+1].keyrecord[j].key)
temp=pvector->record[j];
pvector->record[j]=pvector->record[j+1];
noswap=0;
if(noswap)break;
voidShellSort(SortObject*p,intd,unsignedlong*compare,unsignedlong*exchange)
inti,j,increment,k;
for(increment=d;increment>0;increment/=2)
for(i=increment;in;i++)
j=i-increment;
while(j>=0&&temp.keyrecord[j].key)
pvector->record[j+increment]=pvector->record[j];
j-=increment;
pvector->record[j+increment]=temp;
voidQuickSort(SortObject*pvector,intleft,intright,unsignedlong*compare,unsignedlong*exchange)
inti,j;
if(left>=right)
return;
i=left;
j=right;
while(i!
=j)
while((pvector->record[j].key>=temp.key)&&(j>i))
if(i{pvector->record[i++]=pvector->record[j];(*exchange)++;}while((pvector->record[i].key<=temp.key)&&(j>i)){(*compare)++;i++;}if(i{pvector->record[j--]=pvector->record[i];(*exchange)++;}}pvector->record[i]=temp;(*exchange)++;QuickSort(pvector,left,i-1,compare,exchange);QuickSort(pvector,i+1,right,compare,exchange);}voidSortMethod(void){inti,j,k,l;unsignedlongnum[5][10]={0};unsignedlongsum[10]={0};SortObject*pvector;fflush(stdin);printf("请输入待排序的随机数个数:\n");scanf("%d",&k);pvector=(SortObject*)malloc(sizeof(SortObject));for(j=0;j<5;j++){pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
pvector->record[i++]=pvector->record[j];
while((pvector->record[i].key<=temp.key)&&(j>i))
i++;
if(i{pvector->record[j--]=pvector->record[i];(*exchange)++;}}pvector->record[i]=temp;(*exchange)++;QuickSort(pvector,left,i-1,compare,exchange);QuickSort(pvector,i+1,right,compare,exchange);}voidSortMethod(void){inti,j,k,l;unsignedlongnum[5][10]={0};unsignedlongsum[10]={0};SortObject*pvector;fflush(stdin);printf("请输入待排序的随机数个数:\n");scanf("%d",&k);pvector=(SortObject*)malloc(sizeof(SortObject));for(j=0;j<5;j++){pvector->record=(RecordNode*)malloc(sizeof(RecordNode)*k);for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
pvector->record[j--]=pvector->record[i];
pvector->record[i]=temp;
QuickSort(pvector,left,i-1,compare,exchange);
QuickSort(pvector,i+1,right,compare,exchange);
voidSortMethod(void)
inti,j,k,l;
unsignedlongnum[5][10]={0};
unsignedlongsum[10]={0};
printf("请输入待排序的随机数个数:
\n");
scanf("%d",&k);
pvector=(SortObject*)malloc(sizeof(SortObject));
for(j=0;j<5;j++)
for(i=0;ipvector->record[i].key=rand();pvector->n=k;InsertSort(pvector,&num[j][0],&num[j][1]);SelectSort(pvector,&num[j][2],&num[j][3]);BubbleSort(pvector,&num[j][4],&num[j][5]);ShellSort(pvector,4,&num[j][6],&num[j][7]);QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);}printf("\n排序比较如下");for(j=0;j<5;j++){printf("\n\n对%d个数进行排序,结果为:\n",k);printf("1.插入排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);printf("2.选择排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);printf("3.冒泡排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);printf("4.希尔排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);printf("5.快速排序:比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);if(j!=5)printf("按回车继续\n");getchar();}for(j=0;j<5;j++){sum[0]=sum[0]+num[j][0];sum[1]=sum[1]+num[j][1];sum[2]=sum[2]+num[j][2];sum[3]=sum[3]+num[j][3];sum[4]=sum[4]+num[j][4];sum[5]=sum[5]+num[j][5];sum[6]=sum[6]+num[j][6];sum[7]=sum[7]+num[j][7];sum[8]=sum[8]+num[j][8];sum[9]=sum[9]+num[j][9];}printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:\n",k);printf("1.插入排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);printf("2.选择排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);printf("3.冒泡排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);printf("4.希尔排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);printf("5.快速排序:平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);free(pvector);}voidsort(){inti;while(1){SortMethod();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}voidhuff(){inti;while(1){huffman();printf("\n是否继续?\n1.继续\n2.返回菜单\n");scanf("%d",&i);if(i==2)break;fflush(stdin);getchar();}}main(){inti,j,k;while(1){printf("请选择要运行的功能:\n");printf("1.哈夫曼编码译码器\n");printf("2.内部排序性能分析\n");printf("3.退出该程序\n\n");printf("你的选择为:");scanf("%d",&i);switch(i){case1:huff();break;case2:sort();break;case3:exit(0);default:break;}fflush(stdin);getchar();system("cls");}}
pvector->record[i].key=rand();
pvector->n=k;
InsertSort(pvector,&num[j][0],&num[j][1]);
SelectSort(pvector,&num[j][2],&num[j][3]);
BubbleSort(pvector,&num[j][4],&num[j][5]);
ShellSort(pvector,4,&num[j][6],&num[j][7]);
QuickSort(pvector,0,k-1,&num[j][8],&num[j][9]);
printf("\n排序比较如下");
printf("\n\n对%d个数进行排序,结果为:
\n",k);
printf("1.插入排序:
比较-->%-7ld次移动-->%-7ld次\n",num[j][0],num[j][1]);
printf("2.选择排序:
比较-->%-7ld次移动-->%-7ld次\n",num[j][2],num[j][3]);
printf("3.冒泡排序:
比较-->%-7ld次移动-->%-7ld次\n",num[j][4],num[j][5]);
printf("4.希尔排序:
比较-->%-7ld次移动-->%-7ld次\n",num[j][6],num[j][7]);
printf("5.快速排序:
比较-->%-7ld次移动-->%-7ld次\n",num[j][8],num[j][9]);
=5)
printf("按回车继续\n");
sum[0]=sum[0]+num[j][0];
sum[1]=sum[1]+num[j][1];
sum[2]=sum[2]+num[j][2];
sum[3]=sum[3]+num[j][3];
sum[4]=sum[4]+num[j][4];
sum[5]=sum[5]+num[j][5];
sum[6]=sum[6]+num[j][6];
sum[7]=sum[7]+num[j][7];
sum[8]=sum[8]+num[j][8];
sum[9]=sum[9]+num[j][9];
printf("\n\n对%d个随机数进行5次排序,平均比较次数和平均移动次数为:
平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[0]/5,sum[1]/5);
平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[2]/5,sum[3]/5);
平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[4]/5,sum[5]/5);
平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[6]/5,sum[7]/5);
平均比较-->%-7ld次平均移动-->%-7ld次\n",sum[8]/5,sum[9]/5);
voidsort()
inti;
while
(1)
SortMethod();
printf("\n是否继续?
\n1.继续\n2.返回菜单\n");
scanf("%d",&i);
if(i==2)break;
voidhuff()
huffman();
main()
printf("请选择要运行的功能:
printf("1.哈夫曼编码译码器\n");
printf("2.内部排序性能分析\n");
printf("3.退出该程序\n\n");
printf("你的选择为:
switch(i)
case1:
huff();break;
case2:
sort();break;
case3:
exit(0);
default:
system("cls");
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1