Finding records lost after the merge loop

A program merges two sorted lists of ticket numbers. A tail is the portion of an input that has not yet been consumed. The following Python function contains a bug:


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


Trace the function with left = [2, 7, 11] and right = [4, 6, 10, 13, 15]. The incomplete output contains ticket numbers. At termination, an unconsumed tail remains in the input and contains ticket numbers.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Merge Sort Questions: