归并排序
模板
const int N = 1e6 + 10;
int a[N], tmp[N]; // 定义一个缓存数值
void merge_sort(int q[], int l, int r)
{
if(l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid), merge_sort(q, mid + 1, r);
int k = 0, i = l, j = mid + 1;
while(i <= mid && j <= r)
{
if(q[i] <= q[j]) tmp[k ++ ] = q[i ++ ];
else tmp[k ++ ] = q[j ++ ];
}
while(i <= mid) tmp[k ++ ] = q[i ++ ];
while(j <= r) tmp[k ++ ] = q[j ++ ];
for(k = 0, i = l; i <= r; k ++ , i ++ ) q[i] = tmp[k];
}
标签:sort,归并,int,mid,merge,排序
From: https://www.cnblogs.com/hnu-hua/p/18179461