void merge(int a[], int l, int r) {
if (r - l < 1) //区间长度小于1了
return;
int mid = (l + r) >> 1; //二分
merge(a, l, mid); //左半段
merge(a, mid + 1, r); //右半段
int i = l, j = mid + 1;
fir(k, l, r) if (j > r || i <= mid && a[i] <= a[j]) //将当前最小的放入备用数组.
b[k] = a[i++]; // a[i]被放入,要么是当前最小,要么就是另外一个数组已经空了
else b[k] = a[j++];
fir(k, l, r) a[k] = b[k];
}
。 啊吧啊吧,root我这没发完整答案!!!!!!!!!!!!!!!!!!!!!!!!!!!
共 2 条回复
@baizichen 看不懂思密达
void g(int l, int r) { if (l == r) { return; } int mid = (l + r) / 2; g(l, mid); g(mid + 1, r); int b[100005]; int bxb = 0; int le = l; int ri = mid + 1; while (le <= mid && ri <= r) { if (a[le] <= a[ri]) { bxb++; b[bxb] = a[le]; le++; } else { bxb++; b[bxb] = a[ri]; ri++; } } while (le <= mid) { bxb++; b[bxb] = a[le]; le++; } while (ri <= r) { bxb++; b[bxb] = a[ri]; ri++; } bxb = 1; for (int i = l; i <= r; i++) { a[i] = b[bxb]; bxb++; } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } g(1, n); for (int i = 1; i <= n; i++) { cout << a[i] << " "; }