欢迎来到天天文库
浏览记录
ID:33475524
大小:132.50 KB
页数:12页
时间:2019-02-26
《各种排序算法实现(经典)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、数据结构实验报告各种排序算法的实现(经典)一实验目的使学生掌握常用内部排序的基本概念和基本方法使学生掌握常用内部排序方法的性能评价;使学生掌握排序方法在实际问题中的应用;二实验环境所需硬件环境为微机;所需软件环境为MicrosoftVisualC++6.0;三实验内容此部分仅用写出要求的实验代码,并加上必要的注释#include#defineLIST_INIT_SIZE100#defineLISTINCREMENT10#defineOVERFLOW-2#defineOK1#defineFAILURE0#defineTRUE1#
2、defineFALSE0#defineERROR-1typedefintElemType;typedefintStatus;12数据结构实验报告voidPrintElem(ElemType*e){printf("%5d",*e);}#include#include"ElemType.c"intp[6];//记录比较次数intq[6];//记录移动次数voidInsertSort(int*L,intn)//直接插入排序{inti,j;for(i=2;i<=n;i++){if(L[i]3、]=L[i-1];for(j=i-2;L[0]0&&L[0]4、=2;}p[1]++;}}voidShellSort(int*L,intn)//希尔排序{intdalt[6]={13,11,7,5,3,1};inti;for(i=0;i<6;i++)ShellInsert(L,n,dalt[i]);}voidbuddlesort(int*L,intn)//冒泡排序{inti,j,k,m;for(i=n,k=1;i>1&&k;i--){k=0;for(j=1;jL[j+1]){m=L[j];L[j]=L[j+1];L[j+1]=m;k=1;q[2]+=3;}p[2]++;}}}5、intPartition(int*L,intlow,inthigh){intk;L[0]=L[low];k=L[low];while(low=k){high--;p[3]++;}12数据结构实验报告L[low]=L[high];while(low6、high){intk;if(low=L[j])break;p[4]+=2;q[4]++;L[s]=L[j7、];s=j;}12数据结构实验报告L[s]=t;q[4]+=2;}voidHeapSort(int*L,intn)//堆排序{inti,t;for(i=n/2;i>0;i--)HeapAdjust(L,i,n);for(i=n;i>1;i--){t=L[1];L[1]=L[i];L[i]=t;q[4]+=3;HeapAdjust(L,1,i-1);}}voidMerge(int*SR,int*TR,inti,intm,intn){intj,k,t;for(j=m+1,k=i;i<=m&&j<=n;++k){if(SR[i]8、k]=SR[i];i++;}else{TR[k]=SR[j];j++;}q[5]++;p[5]++;}if(i<=m)for(t=i;t<=m;t++
3、]=L[i-1];for(j=i-2;L[0]0&&L[0]4、=2;}p[1]++;}}voidShellSort(int*L,intn)//希尔排序{intdalt[6]={13,11,7,5,3,1};inti;for(i=0;i<6;i++)ShellInsert(L,n,dalt[i]);}voidbuddlesort(int*L,intn)//冒泡排序{inti,j,k,m;for(i=n,k=1;i>1&&k;i--){k=0;for(j=1;jL[j+1]){m=L[j];L[j]=L[j+1];L[j+1]=m;k=1;q[2]+=3;}p[2]++;}}}5、intPartition(int*L,intlow,inthigh){intk;L[0]=L[low];k=L[low];while(low=k){high--;p[3]++;}12数据结构实验报告L[low]=L[high];while(low6、high){intk;if(low=L[j])break;p[4]+=2;q[4]++;L[s]=L[j7、];s=j;}12数据结构实验报告L[s]=t;q[4]+=2;}voidHeapSort(int*L,intn)//堆排序{inti,t;for(i=n/2;i>0;i--)HeapAdjust(L,i,n);for(i=n;i>1;i--){t=L[1];L[1]=L[i];L[i]=t;q[4]+=3;HeapAdjust(L,1,i-1);}}voidMerge(int*SR,int*TR,inti,intm,intn){intj,k,t;for(j=m+1,k=i;i<=m&&j<=n;++k){if(SR[i]8、k]=SR[i];i++;}else{TR[k]=SR[j];j++;}q[5]++;p[5]++;}if(i<=m)for(t=i;t<=m;t++
4、=2;}p[1]++;}}voidShellSort(int*L,intn)//希尔排序{intdalt[6]={13,11,7,5,3,1};inti;for(i=0;i<6;i++)ShellInsert(L,n,dalt[i]);}voidbuddlesort(int*L,intn)//冒泡排序{inti,j,k,m;for(i=n,k=1;i>1&&k;i--){k=0;for(j=1;jL[j+1]){m=L[j];L[j]=L[j+1];L[j+1]=m;k=1;q[2]+=3;}p[2]++;}}}
5、intPartition(int*L,intlow,inthigh){intk;L[0]=L[low];k=L[low];while(low=k){high--;p[3]++;}12数据结构实验报告L[low]=L[high];while(low6、high){intk;if(low=L[j])break;p[4]+=2;q[4]++;L[s]=L[j7、];s=j;}12数据结构实验报告L[s]=t;q[4]+=2;}voidHeapSort(int*L,intn)//堆排序{inti,t;for(i=n/2;i>0;i--)HeapAdjust(L,i,n);for(i=n;i>1;i--){t=L[1];L[1]=L[i];L[i]=t;q[4]+=3;HeapAdjust(L,1,i-1);}}voidMerge(int*SR,int*TR,inti,intm,intn){intj,k,t;for(j=m+1,k=i;i<=m&&j<=n;++k){if(SR[i]8、k]=SR[i];i++;}else{TR[k]=SR[j];j++;}q[5]++;p[5]++;}if(i<=m)for(t=i;t<=m;t++
6、high){intk;if(low=L[j])break;p[4]+=2;q[4]++;L[s]=L[j
7、];s=j;}12数据结构实验报告L[s]=t;q[4]+=2;}voidHeapSort(int*L,intn)//堆排序{inti,t;for(i=n/2;i>0;i--)HeapAdjust(L,i,n);for(i=n;i>1;i--){t=L[1];L[1]=L[i];L[i]=t;q[4]+=3;HeapAdjust(L,1,i-1);}}voidMerge(int*SR,int*TR,inti,intm,intn){intj,k,t;for(j=m+1,k=i;i<=m&&j<=n;++k){if(SR[i]8、k]=SR[i];i++;}else{TR[k]=SR[j];j++;}q[5]++;p[5]++;}if(i<=m)for(t=i;t<=m;t++
8、k]=SR[i];i++;}else{TR[k]=SR[j];j++;}q[5]++;p[5]++;}if(i<=m)for(t=i;t<=m;t++
此文档下载收益归作者所有