内部排序算法的性能分析.docx
- 文档编号:12448502
- 上传时间:2023-04-19
- 格式:DOCX
- 页数:17
- 大小:17.39KB
内部排序算法的性能分析.docx
《内部排序算法的性能分析.docx》由会员分享,可在线阅读,更多相关《内部排序算法的性能分析.docx(17页珍藏版)》请在冰豆网上搜索。
内部排序算法的性能分析
#include
#include
#include
#defineM50
#defineMAX100000;
typedefstruct
{
intweight;//结点权值
intparent,lchild,rchild;
}HTNODE,*HUFFMANTREE;
typedefchar**HUFFMANCODE;//动态分配数组存储哈夫曼编码表
typedefstruct
{
intkey;/*关键字*/
}RecordNode;/*排序节点的类型*/
typedefstruct
{
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'; else 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; 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;i pvector->record[i]=p->record[i]; pvector->n=p->n; *compare=0; *exchange=0; for(i=1;i { temp=pvector->record[i]; (*exchange)++; j=i-1; while((temp.key { (*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;i pvector->record[i]=p->record[i]; pvector->n=p->n; *compare=0; *exchange=0; for(i=0;i { k=i; for(j=i+1;j { (*compare)++; if(pvector->record[j].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;i pvector->record[i]=p->record[i]; pvector->n=p->n; *compare=0; *exchange=0; for(i=0;i { noswap=1; for(j=0;j { (*compare)++; if(pvector->record[j+1].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;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;i { temp=pvector->record[i]; (*exchange)++; j=i-increment; while(j>=0&&temp.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;i 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排序比较如下"); 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"); } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 内部 排序 算法 性能 分析
![提示](https://static.bdocx.com/images/bang_tan.gif)