归并排序
来源:互联网 发布:知网 高质量 知乎 编辑:程序博客网 时间:2024/06/14 00:16
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include<string.h>
#define maxn 100
#define mem(a,b) memset(a,b,sizeof(a));
using namespace std;
int a[105];int n;int temp[100];
void memger(int a[],int first,int mid,int last,int temp[])
{
int i=first;int j=mid+1;
int k=0;
while(i<=mid&&j<=last)
{
if(a[i]<=a[j])
{
temp[k++]=a[i++];
}else {
temp[k++]=a[j++];}
}
while(i<=mid)
{
temp[k++]=a[i++];
}
while(j<=last)
{
temp[k++]=a[j++];
}
for(int i=0;i<k;i++)
{
a[first+i]=temp[i];
}
}
void memgersort(int a[],int first,int last,int temp[])
{
if(first>=last)
{
return ;
}
int mid=(first+last)/2;
memgersort(a,first,mid,temp);
memgersort(a,mid+1,last,temp);
memger(a,first,mid,last,temp);
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
memgersort(a,0,n-1,temp);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
}
#include <cstdio>
#include <cstring>
#include <string>
#include<string.h>
#define maxn 100
#define mem(a,b) memset(a,b,sizeof(a));
using namespace std;
int a[105];int n;int temp[100];
void memger(int a[],int first,int mid,int last,int temp[])
{
int i=first;int j=mid+1;
int k=0;
while(i<=mid&&j<=last)
{
if(a[i]<=a[j])
{
temp[k++]=a[i++];
}else {
temp[k++]=a[j++];}
}
while(i<=mid)
{
temp[k++]=a[i++];
}
while(j<=last)
{
temp[k++]=a[j++];
}
for(int i=0;i<k;i++)
{
a[first+i]=temp[i];
}
}
void memgersort(int a[],int first,int last,int temp[])
{
if(first>=last)
{
return ;
}
int mid=(first+last)/2;
memgersort(a,first,mid,temp);
memgersort(a,mid+1,last,temp);
memger(a,first,mid,last,temp);
}
int main()
{
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>a[i];
}
memgersort(a,0,n-1,temp);
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
}
阅读全文
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 大数据时代的10个重大变革!
- 一个关于STM32内部USART的波特率配置话题
- JD实战之阶段一
- android 软键盘弹出移动view 键盘监听兼容横竖屏切换
- 数据结构实验之查找三:树的种类统计
- 归并排序
- 比特币,区块链与Fintech
- 用hlsl/glsl实现内发光效果
- ubuntu安装JDK
- 进制的计算 -汇编
- Windows下谷歌浏览器出现ChromeERR_EMPTY_RESPONSE的解决方法
- Android broadcast receiver初体验
- 如何开启API经济学的大门
- 波音公司用区块链打击GPS欺骗攻击