int tmp[];//temp数组存储数据
void merge_sort(int a[],int l,int r) {
if (l >= r) return;//递归到最后只有一个数返回
int mid = (l + r)/2 ;//确定分界点 l~mid mid+1~r;
merge_sort(a, l, mid);
merge_sort(a, mid + 1, r);//递归左右两边
int k = 0, i = l, j = mid + 1;//i,j为指针,分别从左右区的左边界开始移动
while (i<=mid&&j<=r)/移动到右边界停止
{
if (a[i] < a[j]) tmp[k++] = a[i++];
else tmp[k++] = a[j++];//每次选取左右区的较小值填入tmp中
}
while (i <= mid)tmp[k++] = a[i++];//填入左区余留元素
while (j <= r)tmp[k++] = a[j++];//填入右区余留元素
for (i = l, j = 0; i <= r; i++, j++) a[i] = tmp[j];
}
//归并排序
ps:a[i++]=b[j++];先做赋值再做i++,b++;
标签:tmp,sort,归并,merge,++,mid,int,排序 From: https://www.cnblogs.com/dianman/p/18446581