algorithm

how to merge two sorted integer array in place using O(n) time and O(1) space cost


For example, given an integer array and its two consecutive sequence 's beginning position which are 'b1' and 'b2', furthermore provided with the position 'last' which indicates the second sequence's ending position. From array[b1] to array [b2-1] and from array [b2] to array[last] are both in order separately, how to merge them in place using O(n) time and O(1) space cost?


Solution

  • This is by no means a simple problem. It is possible, but rarely done in practice because it's so much more complicated than a standard merge using N-scratch space. Huang and Langston's paper has been around since the late 80's, though practical implementations didn't really surface until later. Earlier, L. Trabb-Pardo's paper in 1977 predates Huang and Langston significantly, but I'm challanged to find the exact text that paper; only references abound.

    An excellent later publication, Asymptotically efficient in-place merging (1995) by Geffert, Katajainen, and Pasanen is a good coverage of multiple algorithms, and references Trabb-Pardo's contributions to the subject.