归并排序
来源:互联网 发布:域名被别人注册了 编辑:程序博客网 时间:2024/06/07 23:51
#include<iostream>
using namespace std;
int Merge(int a[],int s,int m,int e,int temp[])
{
int pb=0;
int p1=s,p2=m+1;
while(p1<=m&&p2<=e)
{
if(a[p1]<a[p2])
temp[pb++]=a[p1++];
else
temp[pb++]=a[p2++];
}
while(p1<=m) //把后面剩下的连接在后面
temp[pb++]=a[p1++];
while(p2<=e)
temp[pb++]=a[p2++];
int i;
for(i=0; i<e-s+1; i++)
a[s+i]=temp[i];
}
int MergeSort(int a[],int s,int e,int temp[])
{
//递归的终止条件为s=e;
if(s<e)
{
int m=s+(e-s)/2;
MergeSort(a,s,m,temp);
MergeSort(a,m+1,e,temp);
Merge(a,s,m,e,temp);
}
}
int main()
{
int a[10],b[10];
int i=0;
for(i=0; i<10; i++)
cin>>a[i];
int size=sizeof(a)/sizeof(int);
MergeSort(a,0,size-1,b);
for(i=0; i<10; i++)
{
if(i)
cout<<" ";
cout<<a[i];
}
cout<<endl;
return 0;
}
using namespace std;
int Merge(int a[],int s,int m,int e,int temp[])
{
int pb=0;
int p1=s,p2=m+1;
while(p1<=m&&p2<=e)
{
if(a[p1]<a[p2])
temp[pb++]=a[p1++];
else
temp[pb++]=a[p2++];
}
while(p1<=m) //把后面剩下的连接在后面
temp[pb++]=a[p1++];
while(p2<=e)
temp[pb++]=a[p2++];
int i;
for(i=0; i<e-s+1; i++)
a[s+i]=temp[i];
}
int MergeSort(int a[],int s,int e,int temp[])
{
//递归的终止条件为s=e;
if(s<e)
{
int m=s+(e-s)/2;
MergeSort(a,s,m,temp);
MergeSort(a,m+1,e,temp);
Merge(a,s,m,e,temp);
}
}
int main()
{
int a[10],b[10];
int i=0;
for(i=0; i<10; i++)
cin>>a[i];
int size=sizeof(a)/sizeof(int);
MergeSort(a,0,size-1,b);
for(i=0; i<10; i++)
{
if(i)
cout<<" ";
cout<<a[i];
}
cout<<endl;
return 0;
}
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- pip window安装
- 循环删除数组元素的几种姿势
- hibernate 初级04
- windows下局域网内复制整个文件夹脚本bat
- CentOS的vsftp修改默认配置路径方法
- 归并排序
- (五)ECMAScript
- 从产品角度,密码安全登陆之app端和网页端
- 关于vector push_back()与其他方式读取数据的效率对比
- 求余
- 在Linux上使用Jenkins持续集成JavaWeb项目
- 4.7.笔记
- string的用法以及cin用法
- JDK各版本之间的特性区别