排序程序小结冒泡排序并归排序插入排序等精.docx

上传人:b****8 文档编号:9438512 上传时间:2023-02-04 格式:DOCX 页数:11 大小:36.96KB
下载 相关 举报
排序程序小结冒泡排序并归排序插入排序等精.docx_第1页
第1页 / 共11页
排序程序小结冒泡排序并归排序插入排序等精.docx_第2页
第2页 / 共11页
排序程序小结冒泡排序并归排序插入排序等精.docx_第3页
第3页 / 共11页
排序程序小结冒泡排序并归排序插入排序等精.docx_第4页
第4页 / 共11页
排序程序小结冒泡排序并归排序插入排序等精.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

排序程序小结冒泡排序并归排序插入排序等精.docx

《排序程序小结冒泡排序并归排序插入排序等精.docx》由会员分享,可在线阅读,更多相关《排序程序小结冒泡排序并归排序插入排序等精.docx(11页珍藏版)》请在冰豆网上搜索。

排序程序小结冒泡排序并归排序插入排序等精.docx

排序程序小结冒泡排序并归排序插入排序等精

程序在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"sortall.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(nChildnTemp

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[O],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;jif(array[j]array[j]ntemp=j;

swap_i(array[i],array]ntemp];

/****************Shell申序****************/voidsort_all:

:

sort_shell(int*array,intlenintstep=len;

inti;

while(step>1step=(step+1/2;

for(i=0;iif(array[i+step]

sort_main.cpp文件

#inelude"sort_all.h"

intmain(

{

intinput[]={2,4,5,1,5,8,10,-2,4};

intlen=sizeof(input/sizeof(int;

intN;

sort_allinstanee;

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;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1