I am trying to sort data in the array recursively.I kept running into the error message saying list index out of range. error message
A=[] f = open(sys.argv[1], "r") for row in f: A.append(row) def divide(p, r): x = A[r] i =p-1 for j in range(0,r-1): if (A[j] <= x): i+=1 temp=A[i] #>>error! list index out of range A[i]=A[j] A[j]=temp temp2=A[i+1] A[i+1]=A[r] A[r]=temp2 return i+1 def sort(p, r): if (p < r) : q = divide(p,r) sort(p, q-1) sort(q+1,r) sort(0,len(A)-1) for a in A: print(a) I write this program by implementing the pseudocode below, and I am confuse about the purpose of the "i" variable.
function sort205(p, r) { if (p < r) { q = divide(p,r); sort205(p, q-1); sort205(q+1, r); } } function divide(p, r) { x = A[r]; i = p-1; for j = p to r-1 { if (A[j] <= x) { i += 1; exchange A[i] with A[j]; } } exchange A[i+1] with A[r] return (i+1); } https://stackoverflow.com/questions/66095394/recursive-sorting-in-python-list-index-out-of-range February 08, 2021 at 11:00AM
没有评论:
发表评论