0

This seems to be working now... Open criticism welcome. Ty tsroten.

debug = 1
#List to be merged
A=[1,2,3,4,5,6,1,2,3,4,5,6,7]

#Artificial variables from calling function
p=0
r=len(A)
q=int(((p+r)/2)-1)

#List to append merged values
B=[]

#End of list
end=len(A)

if debug:
    print ('Mid: %d\n' % (q))
    print ('End: %d' % (end))

#Left and Right lists with sentinels
L=A[:q+1]
R=A[q+1:]
L.append(None)
R.append(None)

#Init counters
i=k=j=l=0

if debug:
    print ('A:%s' %(A))
    print ('\n')
    print ('L:%s' %(L))
    print ('R:%s' %(R))
    print ('\n')

#Merge Left and Right lists
for k in A:
    if debug:
        print ('Iteration:%d' % (l))
        l+=1
        print ('i=%d, j=%d' % (i, j))
        print ('B:%s' %(B))
        print ('\n')
    if L[i] is not None and L[i]<=R[j]:
        B.append(L[i])
        i+=1
    else:
        B.append(R[j])
        j+=1

print ('Result:',B)

I am trying to write a merge algorithm in Python but I keep running into this error.

Traceback (most recent call last):
  File "C:/Python34/Scripts/mergeSort.py", line 19, in <module>
    if L[i]<=R[j] and i<mid:
IndexError: list index out of range

I have looked at other algorithms but they don't seem to have the same problem. Please help.

A=[2,4,5,7,1,3,7,8,9]  
B=[]


end=len(A)  
mid=int(end/2)

L=A[:mid]  
R=A[mid+1:]

i=k=j=0

for k in A:  
    if L[i]<=R[j] and i<mid:  
        B.append(L[i])  
        i+=1  
    elif j<end:  
        B.append(R[j])  
        j+=1  

print (B)
2
  • Any issues with B = L + R, or are you just exploring alternatives? Commented Jul 18, 2015 at 1:30
  • It is more of an exercise to refresh my algorithmic knowledge rather than finding the easiest method. I will eventually tie this together for a merge-sort algorithm. Commented Jul 18, 2015 at 13:10

2 Answers 2

2

Your for loop goes through all the items in A, which is longer than L or R. So, at some point, i and j become greater than the length of L and R. At that point, referencing L[i] or R[j] raises an IndexError.

Sign up to request clarification or add additional context in comments.

6 Comments

I see what you are saying, but I had thought the conditionals 'i<mid' and 'j<end' would prevent it from going out of bounds. The counter 'k' isn't used inside the for loop and should run without error. I did add one to both L and R and the bounds error went away, but if I add another integer to the array, it doesn't merge properly.
len(R) != end So, does j < end really help you?
i made the change from j<end to j<len(R) which yields the same result.
The IndexError refers to L[i], not R[j]. Let's say i == mid - 1. Then, it satisfies the conditional and gets incremented so that i == mid. Now, L[i] raises an IndexError.
Why don't you add some basic print() calls to your code to help you debug. Maybe a print(mid) at the beginning. Then, at the beginning of the for loop: a print('i: %s, j: %s' % (i, j)), a print('L[i]: %s' % L[i]), and a print('R[j]: %s' % R[j]).
|
0

You should check whether i and j are inbounds before calling that index.

Comments

Your Answer

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