归并排序


void merge(int a[], int b[], int left, int mid, int right)
{
    for(int k = left; k <= right; k++)
        b[k] = a[k];
    int s1=left, s2=mid+1, t=left;
    while(s1 <= mid && s2 <= right)
    {
        if(b[s1] <= b[s2]) a[t++]=b[s1++];
        else a[t++]=b[s2++];
    }
    while(s1 <= mid) a[t++]=b[s1++];
    while(s2 <= right) a[t++]=b[s2++];
}


void mergesort(int a[], int b[], int left, int right)
{
    if(left < right)
    {
        int mid = (left+right)/2;
        mergesort(a, b, left, mid);
        mergesort(a, b, mid+1, right);
        merge(a, b, left, mid, right);
    }
}

阅读更多
文章标签: merge
个人分类: 其他
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭