插入排序的花费时间 c*n2, c 是常数
伪代码
INSERTION-SORT(A)
for i to A.length
key = A[j]
//Insert A[j] into the sorted sequence A[1... j-1]
i = j - 1
while i > 0 and A[i] > key
A[i+1] = A[i]
i =i - 1
A[i+1] = key
python3.4 :
def insertion_sort(sort_list):
length = len(sort_list)
for i in range(length)[1:]:
key = sort_list[i]
j = i - 1
while j >= 0 and sort_list[j] > key:
sort_list[j+1] = sort_list[j]
j -= 1
sort_list[j+1] = key