Shell 排序显示 "list index out of range",我想不通
Shell sort shows "list index out of range" and I can't figure that out
我在python中写了一个shell排序,它显示list index out of range
,但我没有找到问题
def shellSort(arr):
#choice the proper h to start
N=len(arr)
h=1
while h<N//3:
h=h*3+1
while h >0:
for i in (h,len(arr)):
key=arr[i]
j=i-h
while arr[j]>key and j>=0:
arr[j+h]=arr[j]
j-=h
arr[j+h]=key
h=h//3
#start sorting
arr=[15,5,9,8,17,25,1,63,2,45,62,4,12,8]
shellSort(arr)
print(arr)
for i in (h,len(arr) - 1):
注意它是 0 索引的。
我在python中写了一个shell排序,它显示list index out of range
,但我没有找到问题
def shellSort(arr):
#choice the proper h to start
N=len(arr)
h=1
while h<N//3:
h=h*3+1
while h >0:
for i in (h,len(arr)):
key=arr[i]
j=i-h
while arr[j]>key and j>=0:
arr[j+h]=arr[j]
j-=h
arr[j+h]=key
h=h//3
#start sorting
arr=[15,5,9,8,17,25,1,63,2,45,62,4,12,8]
shellSort(arr)
print(arr)
for i in (h,len(arr) - 1):
注意它是 0 索引的。