归并排序模板
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来暂时存储结果
注意归并的for循环还有while里不要多写++,否则会出错误的