1

I have two sorted arrays

array1 = [0, 3, 4, 31]
array2 = [4, 6, 30]

I try to sort these arrays by using the code below:

def mergeSortedArray(array1, array2):
    if not len(array1):
        return array2

    if not len(array2):
        return array1


    mergedArray = []
    array1Item = array1[0]
    array2Item = array2[0]
    i = 0
    j = 0

    while (i < len(array1)) and (j < len(array2)):

        if array1Item < array2Item:
            mergedArray.append(array1Item)
            array1Item = array1[i + 1]
            i += 1
        else:
            mergedArray.append(array2Item)
            print(j)
            array2Item = array2[j + 1]
            j += 1

    return mergedArray

print(mergeSortedArray([0, 3, 4, 31], [4, 6, 30]))

But the terminal keep telling me that:

line 26, in mergeSortedArray
    array2Item = array2[j + 1]
IndexError: list index out of range

I wonder which part I did wrong! Can someone explain to me? plz~

BTW, what is the syntactically cleanest way to accomplish this?

2
  • you have while i < len(array1) but then you reference array1[i+1]. Just try array1[len(array1)+1] index is inherently more than length of your list. The index of lists in python starts at 0. Commented Jan 13, 2022 at 13:58
  • Also try to provide sample output of what you're trying to achieve. You mention you want to sort but your function is called merge. you sorting and merging Commented Jan 13, 2022 at 14:01

2 Answers 2

1

Make use of Python's features, such as merging two lists with the + operator. Then, simply sort the new list.

>>> array1 = [0, 3, 4, 31]
>>> array2 = [4, 6, 30]
>>> merged_array = array1 + array2
>>> merged_array.sort()
>>> merged_array
[0, 3, 4, 4, 6, 30, 31]
Sign up to request clarification or add additional context in comments.

Comments

0

before assigning the array1 and array2 next index value to array1Item and array2Item check if do they exist or not.

check the first array next index that it has a value or no.

 if i+1<len(array1):
    array1Item = array1[i + 1]

also check the second array next index value.

if j+1 < len(array2):
   array2Item = array2[j + 1]

Note1: Each Array of our function may have deferent length. Once the comparison is finished and there is no value to compare in one of the arrays، we need to add the remaining values in the latter array to "mergedArray".

def mergeSortedArray(array1, array2):
  if not len(array1):
      return array2

  if not len(array2):
      return array1

  mergedArray = [] 

  array1Item = array1[0]
  array2Item = array2[0]
  i = 0
  j = 0

  while (i < len(array1)) and (j < len(array2)):

      if array1Item < array2Item:
          mergedArray.append(array1Item)
          if i+1<len(array1):
              array1Item = array1[i + 1]
          i += 1
      else:
          mergedArray.append(array2Item)
          if j+1 < len(array2):
              array2Item = array2[j + 1]
          j += 1

  return mergedArray+array1[i:]+array2[j:]
print(mergeSortedArray([0, 12,19,26,4431], [4, 16, 30,40,111,134]))

Note2: for cleaner code even you do not need the array1Item and array2Item variable, your directly use the mergedArray.append(array1[i]) instead of

array1Item = array1[0]
 mergedArray.append(array1Item)
 if i+1<len(array1):
     array1Item = array1[i + 1]

clean code.

def mergeSortedArray(array1, array2):
    if not len(array1):
        return array2

    if not len(array2):
        return array1


    mergedArray = []
    array1Item = array1[0]
    array2Item = array2[0]
    i = 0
    j = 0
    while i < len(array1) and j < len(array2):
         if array1[i] < array2[j]:
           mergedArray.append(array1[i])
           i += 1
         else:
           mergedArray.append(array2[j])
           j += 1
    return mergedArray+ array1[i:] + array2[j:]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.