排序程序小结冒泡排序并归排序插入排序等精.docx
- 文档编号:28636665
- 上传时间:2023-07-19
- 格式:DOCX
- 页数:12
- 大小:15.53KB
排序程序小结冒泡排序并归排序插入排序等精.docx
《排序程序小结冒泡排序并归排序插入排序等精.docx》由会员分享,可在线阅读,更多相关《排序程序小结冒泡排序并归排序插入排序等精.docx(12页珍藏版)》请在冰豆网上搜索。
排序程序小结冒泡排序并归排序插入排序等精
程序在VC6.0下运行测试通过,包含三个文件:
sort_all.h排序类的定义;
sort_real.cpp排序类成员函数定义;sort_main.h运行的主函数。
包括冒泡排序、并归排序、插入排序、选择排序、快速排序、堆排序、Shell排序
——————sort_all.h文件——————————————————
#include
usingnamespacestd;
/****************************************/
classsort_all
{
public:
voidswap_i(int&a,int&b;
voiddisp_array(int*array,intlen;
voiddisp_num(;
voidsort_maopao(int*array,intlen;
voidsort_quick(int*array,intstart,intend;
voidsort_merge(int*array,intstart,intend;
voidsort_heap(int*array,intlen;
voidsort_select(int*array,intlen;
voidsort_insert(int*array,intlen;
voidsort_shell(int*array,intlen;
};
——————————————————————————————————————
————————sort_real.cpp文件——————————————————————
#include"sort_all.h"
voidsort_all:
:
disp_array(int*array,intlen
{
for(inti=0;i
{
cout< } cout< } voidsort_all: : swap_i(int&a,int&b { inttemp; temp=a; a=b; b=temp; } voidsort_all: : disp_num( { cout<<"---冒泡排序,输入1---"<<"\n"; cout<<"---并归排序,输入2---"<<"\n"; cout<<"---插入排序,输入3---"<<"\n"; cout<<"---选择排序,输入4---"<<"\n"; cout<<"---快速排序,输入5---"<<"\n"; cout<<"----堆排序,输入6----"<<"\n"; cout<<"--Shell排序,输入7---"<<"\n"; cout<<"-----结束,输入0-----"<<"\n"; } /**************************************************/ /**************堆排序******************************/ /**************************************************/ voidheap_adj(int*array,inti,intlen { intnTemp; intnChild; for(nTemp=array[i];2*i+1 { nChild=2*i+1; if(nChild nChild++; if(nChild array[i]=array[nChild]; else break; array[nChild]=nTemp; } } voidheap_create(int*array,intlen { for(inti=len/2;i>=0;i-- heap_adj(array,i,len; } voidsort_all: : sort_heap(int*array,intlen { heap_create(array,len; for(inti=len-1;i>0;i-- { swap_i(array[0],array[i]; heap_adj(array,0,i; } } /*******************************************************/ /******************插入排序*****************************/ /*******************************************************/ voidsort_all: : sort_insert(int*array,intlen { inti,j; inttemp; for(i=1;i { temp=array[i]; for(j=i;j>0&&array[j-1]>temp;j-- array[j]=array[j-1]; array[j]=temp; } } /********************************************************/ /*************冒泡排序***********************************/ /********************************************************/ voidsort_all: : sort_maopao(int*array,intlen { inti,j; for(i=0;i { for(j=len-1;j>=i;j-- { if(array[j]>array[j+1] swap_i(array[j],array[j+1]; } } } /**********************************************/ /**************并归排序************************/ /**********************************************/ voidmerge(int*array,intstart,intmid,intend { intlen_A=mid-start+1; intlen_B=end-mid; int*A=newint[len_A]; int*B=newint[len_B]; inti,j; for(i=0;i A[i]=array[i+start]; for(i=0;i B[i]=array[i+mid+1]; i=0; j=0; inttemp; intk=start; while(i { if(A[i]>B[j] { temp=A[i]; i++; } else { temp=B[j]; j++; } array[k++]=temp; } while(i { array[k++]=A[i++]; } while(j { array[k++]=B[j++]; } } voidsort_all: : sort_merge(int*array,intstart,intend { if(start==end return; else { intmid=(start+end/2; sort_merge(array,start,mid; sort_merge(array,mid+1,end; merge(array,start,mid,end; } } /********************快速排序******************************/ /**********************************************************/ voidsort_all: : sort_quick(int*array,intstart,intend { intkey=array[start]; inti=start; intj=end; if(i>=j return; while(i { while(i j--; array[i]=array[j]; while(i=array[i] i++; array[j]=array[i]; } array[i]=key; sort_quick(array,start,i-1; sort_quick(array,i+1,end; } /****************************************************/ /****************选择排序****************************/ /****************************************************/ voidsort_all: : sort_select(int*array,intlen { inti,j; intntemp; intkey; for(i=0;i { key=array[i]; ntemp=i; for(j=i+1;j { if(array[j] ntemp=j; } swap_i(array[i],array[ntemp]; } } /*****************************************/ /****************Shell排序****************/ /*****************************************/ voidsort_all: : sort_shell(int*array,intlen { intstep=len; inti; while(step>1 { step=(step+1/2; for(i=0;i { if(array[i+step] swap_i(array[i+step],array[i]; } } } ————————————————————————————————————————————————————sort_main.cpp文件————————————————— #include"sort_all.h" intmain( { intinput[]={2,4,5,1,5,8,10,-2,4}; intlen=sizeof(input/sizeof(int; intN; sort_allinstance; while(1 { instance.disp_num(; cout<<"请输入: "; cin>>N; if(N==0 break; cout<<"--------------------------------"<<"\n"; cout<<"原始数据: "<<"\n"; instance.disp_array(input,len; switch(N { case1: instance.sort_maopao(input,len; cout<<"冒泡排序结果: "<<"\n"; break; case2: instance.sort_merge(input,0,len-1; cout<<"并归排序结果: "<<"\n"; break; case3: instance.sort_insert(input,len; cout<<"插入排序结果: "<<"\n"; break; case4: instance.sort_select(input,len; cout<<"选择排序结果: "<<"\n"; break; case5: instance.sort_quick(input,0,len-1; cout<<"快速排序结果: "<<"\n"; break; case6: instance.sort_heap(input,len; cout<<"堆排序结果: "<<"\n"; break; case7: instance.sort_shell(input,len; cout<<"Shell排序结果: "<<"\n"; default: break; } instance.disp_array(input,len; cout<<"-------------------------------------"<<"\n"; cout< } return0; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 排序 程序 小结 冒泡 插入