插入排序InsertionSort(Python实现)

如果你对很多种排序都想要有个了解,可以点这里
如果你对插入排序的C++实现很感兴趣,可以点这里
如果你对MIPS汇编语言实现插入排序,可以点这里
用Python实现插入排序

def InsertionSort(arr):
    if type(arr) is not type([1]):
        return "ERROR INPUT"
    i = 1
    while i < len(arr):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
        i += 1
    return arr

多语言对比学习,有奇效

你可能感兴趣的:(Python,算法)