In-place merge of two arrays

前端 未结 4 2065
小鲜肉
小鲜肉 2021-02-01 06:54

We have an array of size m+n in which m elements are present, in sorted order, and a second array of size n, again in sorted order. We want both of

4条回答
  •  生来不讨喜
    2021-02-01 07:19

    Do a regular merge sort but in reverse comparing the largest numbers first, storing (reversed) into the end of the first array going backwards. This way, the elements you're merging are never overwritten (that this works is easy to see if you think about it for a moment).

提交回复
热议问题