#include<iostream>
using namespace std;
void merge(int a[],int low,int mid,int high,int b[])
{
int i=low,j=mid+1,k=0;
while(i<=mid&&j<=high)
{
if(a[i]<=a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
{
b[k++]=a[i++];
}
while(j<=high)
{
b[k++]=a[j++];
}
}
void sort(int a[],int low,int high,int b[])
{
int mid;
if (low<high)
{
mid=(low+high)/2;
sort(a,low,mid,b);
sort(a,mid+1,high,b);
merge(a,low,mid,high,b);
}
}
归并排序:递归实现
最新推荐文章于 2024-11-12 12:29:51 发布