I'm new to python and I wanted to know why my program is displaying "IndexError: list index out of range" for line 4. Can anyone please help.
# A is the array and N is the size of the array.
A =[1,78,46,4,34,10,50,2]
N = len(A)
def Algorithm(A,N):
#B <- Array[N]
B = A[N]
B=[0]*N
for i in range(1,N):
B[A[i]]+=1
i=1
#for i <-- 1 to N
for j in range(1,N):
#for k <-- to B[j]
for k in range(0,B[j]):
A[i]=j
i+=1
return
Algorithm(A,N)
print(A)
Error:
2 N = len(A)
3 def Algorithm(A,N):
4 B = A[N]
5 B=[0]*N
6 for i in range(1,N):
IndexError: list index out of range
A
has eight elements.A[8]
does not exist, because Python lists are zero-indexed. Valid indexes forA
are 0 thru 7. – John Gordon