用python实现插入排序

# 插入排序
def insert_sort(elems):
    for i in range(0, len(elems)):
        x = elems[i]
        j = i
        while j > 0 and elems[j-1] > x:
            elems[j] = elems[j-1]
            j -= 1
        elems[j] = x
    return elems

插入排序的思想:

  1. 假设List的前面已经排好序了
  2. 找到排好序的后面那一个元素x,与他前面的元素比较,如果前面的大,就把前面的赋值给后面,直到找到一个比他小的
  3. 把当前元素插入进去
原文地址:https://www.cnblogs.com/theodoric008/p/8033018.html