欢迎来到天天文库
浏览记录
ID:34714544
大小:48.11 KB
页数:6页
时间:2019-03-10
《数据结构7种排序算法》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#defineMAXSIZE20typedefintKeyType;typedefchar*InfoType;usingnamespacestd;structRedType{KeyTypekey;InfoTypeotherinfo;};structSqList{RedType*r;intlength;};voidinsertsort(SqList&L){for(inti=2;i<=L.length;++i){intj;if(L.r[i].key<=L.r[i-1].key){L.r[0]=L.r[i];for(j=i
2、-1;(L.r[0].key<=L.r[j].key);--j)L.r[j+1]=L.r[j];L.r[j+1]=L.r[0];}}}intSelectMinKey(SqListL,inti){KeyTypemin=L.r[i].key;intk=i;for(intj=i;j<=L.length;++j)if(min>L.r[j].key){min=L.r[j].key;k=j;}returnk;}voidswap(RedType*i,RedType*j){RedTypetemp;temp=*i;*i=*j;*j=temp;}voidSelectSort(SqList&L){for(
3、inti=1;i=L.r[j+1].key)swap(L.r[j],L.r[j+1]);}intpartition(SqList&L,intlow,inthigh){L.r[0]=L.r[low];intpivotkey=L.r[low].key;while(lo
4、w=pivotkey))--high;L.r[low]=L.r[high];while((low5、votkey-1);}}voidQuickSort(SqList&L){Qsort(L,1,L.length);}voidprintlist(SqListL){for(inti=1;i<=L.length;i++)printf("%d",L.r[i].key);printf("");}typedefSqListHeapType;voidHeapAdjust(HeapType&H,ints,intm){RedTyperc=H.r[s];for(inti=2*s;i<=m;i*=2){if((i6、=H.r[i].key)break;else{H.r[s]=H.r[i];//将较大的孩子与其自身交换s=i;}}H.r[s]=rc;}voidHeapSort(HeapType&H){for(inti=H.length/2;i>0;--i)HeapAdjust(H,i,H.length);for(inti=H.length;i>1;--i){printf("%d",H.r[1].key);swap(H.r[1],H.r[i]);HeapAdjust(H,1,i-1);}printf("%d",H.r[1].key);printf("");}voidMerge(RedTypeSR7、[],RedTypeTR[],inti,intm,intn){intj,k;for(j=m+1,k=i;i<=m&&j<=n;++k){if(SR[i].key<=SR[j].key)TR[k]=SR[i++];elseTR[k]=SR[j++];}while(i<=m)TR[k++]=SR[i++];while(j<=n)TR[k++]=SR[j++];}voidMSort(RedTypeSR[],RedTypeTR1[],ints,intt){RedT
5、votkey-1);}}voidQuickSort(SqList&L){Qsort(L,1,L.length);}voidprintlist(SqListL){for(inti=1;i<=L.length;i++)printf("%d",L.r[i].key);printf("");}typedefSqListHeapType;voidHeapAdjust(HeapType&H,ints,intm){RedTyperc=H.r[s];for(inti=2*s;i<=m;i*=2){if((i
6、=H.r[i].key)break;else{H.r[s]=H.r[i];//将较大的孩子与其自身交换s=i;}}H.r[s]=rc;}voidHeapSort(HeapType&H){for(inti=H.length/2;i>0;--i)HeapAdjust(H,i,H.length);for(inti=H.length;i>1;--i){printf("%d",H.r[1].key);swap(H.r[1],H.r[i]);HeapAdjust(H,1,i-1);}printf("%d",H.r[1].key);printf("");}voidMerge(RedTypeSR
7、[],RedTypeTR[],inti,intm,intn){intj,k;for(j=m+1,k=i;i<=m&&j<=n;++k){if(SR[i].key<=SR[j].key)TR[k]=SR[i++];elseTR[k]=SR[j++];}while(i<=m)TR[k++]=SR[i++];while(j<=n)TR[k++]=SR[j++];}voidMSort(RedTypeSR[],RedTypeTR1[],ints,intt){RedT
此文档下载收益归作者所有