合并2个大小相同且没有额外空间的排序数组

Merge 2 sorted arrays with the same size without extra space?

本文关键字:空间 数组 排序 2个 合并      更新时间:2023-10-16

下面是我要做的一个例子:

int size = 10;
int *data = new int[size];
int *array_1 = &data[0];
int *array_2 = &data[size/2];
//fill array_1 and array_2 with data
sort(array_1, array_1+size/2);
sort(array_2, array_2+size/2);
//now, is it possible to merge the 2 sorted arrays?

例如array_1 = {1, 4, 7}array_2 = {3, 5, 6}

我想让data = {1, 3, 4, 5, 6, 7}

您需要std::inplace_merge。见http://www.cplusplus.com/reference/algorithm/inplace_merge/