python 代码实现插入排序

def insert_sort(alist):
    '''插入排序'''
    n = len(alist)
    for j in range(1,n):
        i = j
        while i > 0:
            if alist[i] < alist[i-1]:
                alist[i], alist[i-1] = alist[i-1],alist[i]
                i -= 1
            else:
                break

if __name__ == '__main__':
    li = [1, 30, -6, 0, 98, 99, 4]
    print(li)
    insert_sort(li)
    print(li)

C:\Users\user\AppData\Local\Programs\Python\Python36\python.exe “C:/Users/user/PycharmProjects/hellow python/test.py”
[1, 30, -6, 0, 98, 99, 4]
[-6, 0, 1, 4, 30, 98, 99]

Process finished with exit code 0

上一篇:leetcode计划


下一篇:UICC 之 USIM 详解全系列——USIM App关键流程介绍与命令详解