归并排序[模板]

void merge_sort(int a[], int tmp[], int l, int r)
{
    if (l >= r) return;
    
    int mid = l + r >> 1;
    merge_sort(a, tmp, l, mid);
    merge_sort(a, tmp, mid + 1, r);
    
    int k = 0, i = l, j = mid + 1;
    while (i <= mid && j <= r)
    {
        if (a[i] < a[j]) tmp[k++] = a[i++];
        else tmp[k++] = a[j++];
    }
    while (i <= mid) tmp[k++] = a[i++];
    while (j <= r) tmp[k++] = a[j++];
    
    for (i = l, k = 0; i <= r; i++, k++)
        a[i] = tmp[k];
}

你可能感兴趣的:(归并排序[模板])