def insert_sort(old):
for i in range(1, len(old)):
for j in range(i, 0, -1):
if(old[j] < old[j-1]):
tmp = old[j]
old[j] = old[j-1]
old[j-1] = tmp # test case
old = [2, 5, 3, 0, 2, 3, 0, 3]
insert_sort(old)
print old
插入排序的时间复杂度O(N^2)
2024-03-22 09:42:52
def insert_sort(old):
for i in range(1, len(old)):
for j in range(i, 0, -1):
if(old[j] < old[j-1]):
tmp = old[j]
old[j] = old[j-1]
old[j-1] = tmp # test case
old = [2, 5, 3, 0, 2, 3, 0, 3]
insert_sort(old)
print old
插入排序的时间复杂度O(N^2)