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 l = L; int r = mid + 1; int n = 0; while (l <= mid && r <= R) { if (q[l] < q[r]) Temp[n++] = q[l++]; else Temp[n++] = q[r++]; } while (l <= mid)Temp[n++] = q[l++]; while (r <= R)Temp[n++] = q[r++]; for (int i = L,j = 0; L <= R;L++,j++)//数据导入 q[L] = Temp[j]; }
注意归并排序需要额外一个数组Temp来暂时存储结果
标签:sort,归并,int,mid,merge,排序,模板 From: https://www.cnblogs.com/WKWKSL/p/17347710.html