各种排序算法数据结构中的排序算法.docx
- 文档编号:27995290
- 上传时间:2023-07-07
- 格式:DOCX
- 页数:18
- 大小:17.15KB
各种排序算法数据结构中的排序算法.docx
《各种排序算法数据结构中的排序算法.docx》由会员分享,可在线阅读,更多相关《各种排序算法数据结构中的排序算法.docx(18页珍藏版)》请在冰豆网上搜索。
各种排序算法数据结构中的排序算法
1.直接插入排序
//InsertSort.cpp
//ThisfunctionistosortSqList
#include
#include
#defineMAXSIZE20
#defineMAX_LENGTH100
typedefintRedType;
typedefstruct//definestructureSqList
{RedTyper[MAXSIZE+1];
intlength;
}SqList;
voidInsertSort(SqList&L)//InsertSort()sub-function
{inti,j;
for(i=2;i<=L.length;++i)
if(L.r[i] {L.r[0]=L.r[i]; for(j=i-1;L.r[0] L.r[j+1]=L.r[j]; L.r[j+1]=L.r[0]; } } voidmain()//main()function {inti; SqListL; cout< cout< cout< "< cin>>L.length; for(i=1;i<=L.length;++i) {cout<<"Pleaseinputthe"< "; cin>>L.r[i]; } cout< "; for(i=1;i<=L.length;i++) cout< InsertSort(L);//callInsertSort() cout< "; for(i=1;i<=L.length;i++) cout< cout< ..."; getch(); }//main()end 2.希尔排序 //Shellinert.cpp //ThisfunctionisShellsort #include #include #defineMAXSIZE20 #defineOK1 #defineERROR0 typedefintRedType; typedefstruct//structureSqList {RedTyper[MAXSIZE+1]; intlength; }SqList; voidShellinsert(SqList&L,intdk)//Shellinsert()sub-function {inti,j; for(i=dk+1;i<=L.length;++i) if(L.r[i] {L.r[0]=L.r[i]; for(j=i-dk;j>0&&(L.r[0] L.r[j+dk]=L.r[j]; L.r[j+dk]=L.r[0]; } } voidmain()//main()function {inti,dk=5; SqListL={{0,49,38,65,97,76,13,27,49,55,4},10}; cout< cout< cout< "; for(i=1;i<=L.length;i++) cout< Shellinsert(L,dk);//callShellinsert() cout< "; for(i=1;i<=L.length;i++) cout< cout< ..."; getch(); }//main()end 3.冒泡排序 //BubbleSort.cpp //ThisfunctionistosortSqList #include #include #defineMAXSIZE20 #defineMAX_LENGTH100 typedefintRedType; typedefstruct//definestructureSqList {RedTyper[MAXSIZE+1]; intlength; }SqList; voidBubbleSort(SqList&L) {inti,j,temp; for(i=0;i<=L.length;++i) for(j=L.length-2;j>i;--j) if(L.r[j+1] {temp=L.r[j+1]; L.r[j+1]=L.r[j]; L.r[j]=temp; } } voidmain()//main()function {inti; SqListL; cout< cout< cout< "; cin>>L.length; L.length++;//thelastisaidedspace for(i=1;i {cout<<"Pleaseinputthe"< "; cin>>L.r[i]; } cout< "; for(i=1;i cout< BubbleSort(L);//callBubbleSort() cout< "; for(i=1;i cout< cout< ..."; getch(); }//main()end 4.快速排序 //QuickSort.cpp //Thisfunctionistoquick-sortSqList #include #include #defineMAXSIZE20 typedefintRedType; typedefstruct//defineSqListstructure {RedTyper[MAXSIZE+1]; intlength; }SqList; intPartition(SqList&L,intlow,inthigh)//Partition()sub-function {intpivotkey; L.r[0]=L.r[low]; pivotkey=L.r[low]; while(low {while(low --high; L.r[low]=L.r[high]; while(low ++low; L.r[high]=L.r[low]; } L.r[low]=L.r[0]; return(low); }//Partition()end voidQsort(SqList&L,intlow,inthigh)//Qsort()sub-function {intpivotloc; if(low {pivotloc=Partition(L,low,high); Qsort(L,low,pivotloc-1); Qsort(L,pivotloc+1,high); } } voidQuickSort(SqList&L)//QuickSort()sub-function {Qsort(L,1,L.length);//callQsort() } voidmain()//main()function {inti; SqListL={{0,49,38,65,97,76,13,27,49},8}; cout< cout< cout< "; for(i=1;i<=L.length;i++) cout< QuickSort(L);//callQuickSort() cout< "; for(i=1;i<=L.length;i++) cout< cout< ..."; getch(); }//main()end 5.直接选择排序 //SelectSort.cpp //ThisfunctionistoSelectSortSqList #include #include #defineMAXSIZE20 typedefintRedType; typedefstruct//definestructureSqList {RedTyper[MAXSIZE+1]; intlength; }SqList; voidSelectSort(SqList&L)//SelectSort()sub-function {inti,j,k,temp; for(i=0;i {k=i; for(j=i+1;j if(L.r[j] k=j; if(i! =k) {temp=L.r[i]; L.r[i]=L.r[k]; L.r[k]=temp; } } }//SelectSort()end voidmain()//main()function {inti; SqListL={{49,38,65,97,76,13,27,49,},8}; cout< cout< cout< "; for(i=0;i cout< SelectSort(L);//callSelectSort() cout< "; for(i=0;i cout< cout< ..."; getch(); }//main()end 6.堆栈排序 //HeapSort.cpp //ThisfunctionistoHeapSortSqList #include #include #defineMAXSIZE20 typedefintRedType; typedefstruct//definestructureSqList {RedTyper[MAXSIZE+1]; intlength; }SqList; typedefSqListHeapType; voidHeapAdjust(HeapType&H,ints,intm)//HeapAdjust()sub-function {inttemp,j; temp=H.r[s]; for(j=2*s;j<=m;j*=2) {if(j ++j; if(! (temp break; H.r[s]=H.r[j]; s=j; } H.r[s]=temp; }//HeapAdjust()end voidHeapSort(HeapType&H)//HeapSort()sub-function {inti,temp; for(i=H.length/2;i>0;--i) HeapAdjust(H,i,H.length); for(i=H.length;i>1;--i) {temp=H.r[1]; H.r[1]=H.r[i]; H.r[i]=temp; HeapAdjust(H,1,i-1); } }//HeapSort()end voidmain()//main()function {inti; HeapTypeH={{0,49,38,65,97,76,13,27,49},8}; cout< cout< cout< "; for(i=1;i<=H.length;i++) cout< HeapSort(H);//callHeapSort() cout< "; for(i=1;i<=H.length;i++) cout< cout< ..."; getch(); }//main()end 7.归并排序 //MergeSort.cpp #include #include #defineMAXSIZE20 #defineLENGTH7 typedefintRedType; typedefstruct//SqListstructure {RedTyper[MAXSIZE+1];//RecordsType intlength; }SqList; typedefSqListRcdType; voidMerge(RcdTypeSR,RcdType&TR,inti,intm,intn)//Merge()function {intj,k; for(j=m+1,k=i;i<=m&&j<=n;++k) {if(SR.r[i]<=SR.r[j]) TR.r[k]=SR.r[i++]; else TR.r[k]=SR.r[j++]; } while(i<=m) TR.r[k++]=SR.r[i++]; while(j<=n) TR.r[k++]=SR.r[j++]; }//endofMerge()function voidMSort(RcdTypeSR,RcdType&TR1,ints,intt)//MSort()function {intm; RcdTypeTR2;//[LENGTH]; if(s==t) TR1.r[s]=SR.r[t]; else {m=(s+t)/2; MSort(SR,TR2,s,m); MSort(SR,TR2,m+1,t); Merge(TR2,TR1,s,m,t); }//endofelse }//endofMSort()function voidMergeSort(SqList&L)//MergeSort()function { MSort(L,L,1,L.length); }//endofMergeSort()function voidmain()//mainfunction {inti; SqListL;//={{0,49,38,65,97,76,13,27,},LENGTH}; cout<<"MergeSort.cpp"< cout<<"PleaseinputthelengthofSqListL: cin>>L.length; cout<<"PleaseinputthedisorderedarrayL.r: for(i=1;i<=L.length;i++) cin>>L.r[i]; MergeSort(L); cout< "; for(i=1;i<=L.length;i++) cout< cout< cout<<"...OK! ..."< getch(); }//endofmain()function 8.基数排序 //Radixsort.cpp #include #include #include #defineMAX_NUM_OF_KEY8 #defineRD10 #defineMAX_SPACE10000 #defineERROR-1 typedefintKeyType; typedefintInfoType; typedefintArrType[RD]; typedefstructSLCell {KeyTypekeys[MAX_NUM_OF_KEY]; InfoTypeotheritems; intnext; }SLCell; typedefstructSLList {SLCellr[MAX_SPACE]; intkeynum; intrecnum; }SLList; intSucc(intj)//Succ()function {//Togetthenextfunction j++; return(j); }//endofSucc()function intOrd(intKeyBit)//Ord()function { intj; for(j=0;j<=RD-1&&j! =KeyBit;j++); if(j! =KeyBit)return(ERROR);//KeyBitOVERFLOWTHENERROR elsereturn(j); }//endofOrd()function voidOutExample(SLListL,inti)//OutExample()function { ////////////////////Output//////////////// inttemp,k; printf("\nThe%dCollectresultis: ",i); //temp=L.r[0].otheritems; //printf("%d->",temp); temp=L.r[0].next; printf("%d->",L.r[temp].otheritems); for(k=0;k {temp=L.r[temp].next; printf("%d->",L.r[temp].otheritems); } printf("%d",L.r[L.r[temp].next].otheritems); printf("\n"); ////////////////////////////////////////////// }//endofOutExample()function voidDistribute(SLList&L,inti,ArrType&f,ArrType&e)//Distribute()function {intj,p; for(j=0;j f[j]=0; for(p=L.r[0].next;p;p=L.r[p].next) {j=Ord(L.r[p].keys[i]);//callOrd() if(! f[j]) f[j]=p; else L.r[e[j]].next=p; e[j]=p; }//endoffor }//endofDistrubute()function voidCollect(SLList&L,inti,ArrTypef,ArrTypee)//Collect()function {intj,t; for(j=0;! f[j];j=Succ(j));//Succ() L.r[0].next=f[j]; t=e[j]; while(j {for(j=Succ(j);j f[j];j=Succ(j)); if(f[j]) {L.r[t].next=f[j]; t=e[j]; }//endofif }//endofwhile L.r[t].next=0; OutExample(L,i);//AddOutputExamplefunctionhere }//endofCollect()function voidRadixSort(SLList&L) {
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 各种 排序 算法 数据结构 中的
![提示](https://static.bdocx.com/images/bang_tan.gif)