• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C++ 归并排序算法

OC/C/C++ 水墨上仙 2814次浏览

C++ 归并排序算法

//Merge sort 
//programming by : Erfan Nasoori
//Email : ketn68@yahoo.com
//Date of sent : 2009/1/21
#include<iostream.h>
#include<math.h>
//function declaration
void merge(int[],int[],int[],int,int); // MERGE sort function
void bubblesortAsce(int[],int);        // ASCENDING sort function
void bubblesortDesce(int[],int);       // DESCENDING sort function
void main()
{
 int a[100],b[100],c[200],m,n,s,i;
 char key;
 cout<<"Well come to merge sort program of C++ ."<<endl;
do{
        cout<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
	cout<<"How many number do you need for array a?"<< endl;
	cout<<"m=";   cin>>m;
	cout<<"Now please enter m numbers in array a :"<<endl;
	for(i=0;i<m;++i)
	 {
	  cout<<"element["<<(i+1)<<"]=";
	  cin>>a[i];
	 }
	bubblesortAsce(a,m);
	cout<<"a ASCENDING sorted is:"<<endl;
	for(i=0;i<m;++i)
	 {
	  cout<<" "<<a[i]<<" ";
	 }
	cout<<endl;
	cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
	cout<<"How many number do you need for array b?"<<endl;
	cout<<"n=";   cin>>n;
	cout<<"Now please enter n numbers in array b :"<<endl;
	for(i=0;i<n;++i)
	 {
	  cout<<"element["<<(i+1)<<"]=";
	  cin>>b[i];
	 }
        bubblesortDesce(b,n);
        cout<<"b DESCENDING sorted is:"<<endl;
        for(i=0;i<n;++i)
	 {
	  cout<<" "<<b[i]<<" ";
	 }
        cout<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"array c is merged array."<<endl;
        merge(a,b,c,m,n);
        for(i=0;i<m+n;++i)
	{
	 cout<<" "<<c[i]<<" ";
	}
        cout<<endl;
        cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        cout<<"\n\nDo you want to continue to work by program?"<<endl;
        cout<<"If you want it press y ,whereas press any key."<<endl;
        cin>>key;
 }
 while(key=='y' || key=='Y');
}
//function implementation/////////////////////
void bubblesortAsce(int x[],int y)
{
int i,j,hold;
for(i=1;i<=y-1;++i)
 for(j=0;j<y-i;++j)
 {
  if(x[j] > x[j+1])
  {
    hold=x[j];
    x[j]=x[j+1];
    x[j+1]=hold;
  }
 }
}
//function implementation/////////////////////
void bubblesortDesce(int z[],int w)
{
 int i,j,hold;
 for(i=1;i<=w-1;++i)
  for(j=0;j<w-i;++j)
  {
    if(z[j] < z[j+1])
    {
      hold=z[j];
      z[j]=z[j+1];
      z[j+1]=hold;
    }
  }
}
//function implementation/////////////////////
void merge(int x[],int z[],int ME[],int y,int w)
{
 int i,j,l,s;
 s=w+y;
 for(i=0,j=w-1,l=0 ; i<y && j>=0 ;     )
  if(x[i]<z[j])
  {
    ME[l]=x[i];
    ++i;
    ++l;
  }
  else
  {
    ME[l]=z[j];
    --j;
    ++l;
  }
 if(y>w)
  for(; i<y;++i,l++)
   ME[l]=x[i];
 else
  for(; j>=0; l++,--j)
   ME[l]=z[j];
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C++ 归并排序算法
喜欢 (0)
加载中……