0

Many of us know about the merge subroutine from mergesort. Here, in python:

def merge(left, right):
    result = []
    i ,j = 0, 0
    while i < len(left) and j < len(right):
        if left[i] <= right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1

Is there a way I could write merge without an additional array (without result in this case)? I have tried making use of the fact that a string $s = xy = (y^Rx^R)^R$ (where R means reverse) to no avail.

This in-place version of merge would assume that the two input subarrays are adjacent. (The first array may be from [i, j) and the second may be from [j, k) for 3 input indices i, j, and k.)

By the way, I am open to using languages other than python such as C++ or Java.

dangerChihuahua007
  • 20,299
  • 35
  • 117
  • 206

0 Answers0