【发布时间】:2021-03-30 02:51:30
【问题描述】:
所以,我尝试得到 [1, 2, 3, 5, -4] 但我想得到 [3, 1, 2, 5, -4] 作为结果;因为我只想从列表的索引 1 到索引 4 排序(包括开始,不包括结束;这意味着只从索引 1 到索引 3 排序,保持索引 0 和 4 不变)
def insertionSort(the_list, start, end):
for mark in range(start, end):
temp = the_list[mark]
i = mark-1
while i >= 0 and the_list[i] > temp:
the_list[i+1] = the_list[i]
i -= 1
the_list[i+1] = temp
return the_list
print(insertionSort([3, 2, 5, 1, -4], 1, 4))
有人可以帮我修复代码,以便我得到我想要的结果吗?谢谢。
【问题讨论】:
标签: python insertion-sort