def insertsort(array):
for removed_index in range(1, len(array)):
removed_value = array[removed_index]
insert_index = removed_index
while insert_index > 0 and array[insert_index - 1] > removed_value:
array[insert_index] = array[insert_index - 1]
insert_index -= 1
array[insert_index] = removed_value
另外一个版本:
def insertsort(array):
for lastsortedelement in range(len(array)-1):
checked = lastsortedelement
while array[checked] > array[lastsortedelement + 1] and checked >= 0:
checked -= 1
#Insert the number into the correct position
array[checked+1], array[checked+2 : lastsortedelement+2] = array[lastsortedelement+1], array[checked+1 : lastsortedelement+1]
return array
希望本文所述对大家的Python程序设计有所帮助。
查看更多关于python插入排序算法实例分析的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did91208