def insertionSort(lst):
k = 1
while k < len(lst):
x = lst.pop(k)
insertInOrder(lst, k, x)
k += 1
def insertInOrder(lst, k, x):
while k >= 1 and lst[k-1] > x:
k -= 1
lst.insert(k, x)
def insertionSort(lst):
k = 1
while k < len(lst):
x = lst.pop(k)
insertInOrder(lst, k, x)
k += 1
def insertInOrder(lst, k, x):
print(k, x, lst)
while k > 0 and lst[k-1] > x:
k -= 1
lst.insert(k, x)
a = [4, 5, 7, 2, 9]
print(a)
insertionSort(a)
print(a)
在
insertInOrder
中的第一个while
测试中有一个小错误。这是修复过的版本。我添加了一个print
调用,这样您就可以看到排序过程。你知道吗输出
相关问题 更多 >
编程相关推荐