【问题标题】:Shell sort shows "list index out of range" and I can't figure that out外壳排序显示“列表索引超出范围”,我无法弄清楚
【发布时间】:2020-06-04 01:46:41
【问题描述】:

我用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)

【问题讨论】:

    标签: python shellsort


    【解决方案1】:
    for i in (h,len(arr) - 1):
    

    注意它是 0-indexed。

    【讨论】:

    • @Michael_Zhu 如果满足您的问题,请将答案标记为已接受。通过这样做,您可以帮助其他人更快地找到解决方案。
    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 2011-08-13
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多