33,008
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "stdio.h"
#include "iostream"
using namespace std;
//src[l..m] and src[m+1 ... h]
void merge(int src[],int dst[],int l,int m,int h)
{
int i=l,j=m+1,k=l;
while(i<=m&&j<=h)
{
if (src[i]<src[j])
{
dst[k++]=src[i++];
}
else
{
dst[k++]=src[j++];
}
}
while(i<=m)
{
dst[k++]=src[i++];
}
while(j<=h)
{
dst[k++]=src[j++];
}
}
// put the sorted num into dst[]
void merge_sort(int src[],int dst[],int low,int high)
{
if (low<high)
{
int mid=low+(high-low)/2;
merge_sort(src,dst,low,mid);
merge_sort(src,dst,mid+1,high);
merge(src,dst,low,mid,high);
}
}
void main()
{
int a[4]={4,3,6,1};
int b[4];
merge_sort(a,b,0,3);
}