返回顶部

归并排序模板

int a[10001], c[10001];
void Mergesort(int l, int r)
{
    if (l >= r)
        return;
    int mid = (l + r) / 2;
    Mergesort(l, mid);
    Mergesort(mid + 1, r);
    int i = l, j = mid + 1, k = 1;
    while (i <= mid && j <= r)
    {
        if (a[i] < a[j])
            c[k++] = a[i++];
        else
            c[k++] = a[j++];
    }
    for (int i = l; i <= r; ++i)
        a[i] = c[i];
}

首发于:https://www.luogu.com.cn/blog/zrc4889/

posted @ 2022-01-08 22:22  zrc4889  阅读(21)  评论(0编辑  收藏  举报