首页 / 算法 / C++ 归并排序算法的代码
C++ 归并排序算法的代码
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 归并排序算法的代码,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1912字,纯文字阅读大概需要3分钟。
内容图文
![C++ 归并排序算法的代码](/upload/InfoBanner/zyjiaocheng/721/b38effa68b5c43cd99feb8008c27af19.jpg)
将开发过程中比较好的代码片段做个备份,下面代码内容是关于C++ 归并排序算法的代码。
#include<iostream.h>
#include<math.h>
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<<"nnDo 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');
}
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;
}
}
}
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;
}
}
}
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++ 归并排序算法的代码全部内容,希望文章能够帮你解决C++ 归并排序算法的代码所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。