繁体   English   中英

插入排序Python错误

[英]Error with Insertion Sort Python

我正在尝试使用python实现插入排序。 这是我的代码:

def insertionSort(lst):
    lstSorted = [lst[0]]
    for i in range(1,len(lst)):
        index = len(lstSorted)-1
        while index >= 0:
            if lst[i] > lstSorted[index]:
                lstSorted.insert((index+1),lst[i])
            else:
                lstSorted.insert(index,lst[i])
            index -= 1
    return lstSorted



def main():
    lst = [100,10,10000,1000,1000000,10000]
    sortedLst = insertionSort(lst)
    print(sortedLst)

main()

这是输出:

[10, 10000, 10000, 1000000, 1000, 10000, 10000, 1000000, 1000, 10000, 10000, 1000000, 10000, 10000, 10000, 1000000, 100, 10000, 10000, 1000000, 1000, 10000, 10000, 1000000, 1000, 10000, 10000, 1000000, 10000, 10000, 10000, 1000000]

我意识到我的“ index- = 1”存在错误,但是我不确定如何解决。 当到达以下位置时,这是一个问题:

10, 100, 10000

while循环永远不会中断,因为索引为0,因此while循环再次运行,因此变为:

10, 10000, 100, 10000

我该如何解决?

发生的事情是,每次执行ifif传递时,它将在其前面插入元素。 尝试这个,

def insertionSort(lst):
    for i in range(1,len(lst)):
        j = i
        while j > 0:
            if lst[j-1] > lst[j]:
                t=lst[j]
                lst[j]=lst[j-1]
                lst[j-1]=t
            j-= 1
    return lst

def main():
    lst = [100,10,10000,1000,1000000,10000]
    sortedLst = insertionSort(lst)
    print(sortedLst)

main()

您正在做的是如果lst中的元素大于lstSorted中的元素,则在lstSorted元素之后插入lst元素,但是如果较小,则将其添加到lstSorted中的元素之前,然后递减索引并再次进行比较。 因此,如果列表为[5,4,3]第一个循环lstSorted变为[4,5]但是第二个循环则变为[3,4,3,5]您需要做的是选择适当的位置放入lst[i] ,然后将其插入。

因此,如果您想要单独的列表,则可以将脚本修改为

def insertionSort(lst):
    lstSorted = [lst[0]]
    for i in range(1,len(lst)):
        index = i-1
        while index >= 0:
            if lst[i] > lstSorted[index]:
                lstSorted.insert((index+1),lst[i])
                break
            index -= 1
        if index==-1:
                lstSorted.insert(0,lst[i])
    return lstSorted

def main():
    lst = [100,10,10000,1000,1000000,10000]
    sortedLst = insertionSort(lst)
    print(sortedLst)

main()

您遇到的问题是,您正在重复将相同的值插入while循环中的排序列表中。 您只想插入一次(每个for循环周期),因此您需要稍微更改一下逻辑。

最简单的更改是在第一个insert调用之后添加一个break ,然后将else块从if移到while循环(并将插入索引更改为0 )。 这使用了Python的一个相对不寻常的功能,即循环可以具有else块,这些块仅在循环运行完成而没有命中break语句时才运行。

def insertionSort(lst):
    lstSorted = [lst[0]]
    for i in range(1,len(lst)):
        index = len(lstSorted)-1
        while index >= 0:
            if lst[i] > lstSorted[index]:
                lstSorted.insert(index + 1, lst[i])
                break     # stop the while loop after inserting
            index -= 1
        else:             # if we reached index -1 without breaking, insert at the start
            lstSorted.insert(0, lst[i])
    return lstSorted

您可能会注意到,虽然索引0处的insert实际上仍然是index+1 ,就像另一个插入调用一样,所以实际上我们可以将两种情况组合成一个。 我认为这要优雅得多:

def insertionSort(lst):
    lstSorted = [lst[0]]
    for i in range(1,len(lst)):
        index = len(lstSorted)-1
        while True:   # loop until a break is reached
            if index < 0 or lst[i] > lstSorted[index]:   # combine both insert cases
                lstSorted.insert(index + 1, lst[i])
                break
    return lstSorted

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM