插入排序(insertion_sort)python代码

class insertion_sort:
    def __init__(self):
        pass
    def __call__(self,list):
        if list == []:
            return list             #检测是否为空列表

        i = 1
        ls = len(list)
        while list[i] > list[i - 1]:
            if i == ls:
                return list
            i += 1

        i = ls - 2
        while list[i] < list[i + 1]:  # 检测是否有序
            if i == 0:
                j = []
                for i in list:
                    j.insert(i, 1)  # 颠倒
                return j
            i -= 1

        for i in range(1, ls):
            if list[i] < list[i - 1]:
                tmp = list[i]
                j = i - 1
                while tmp < list[j] and j > 0:  # 主部分
                    list[j + 1] = list[j]
                    j -= 1
                list[j + 1] = tmp
        return list
输入:[1,5,9,1,7,2,15,7]
过程:[1, 1, 5, 9, 7, 2, 15, 7]
      [1, 1, 5, 7, 9, 2, 15, 7]
      [1, 1, 2, 5, 7, 9, 15, 7]
      [1, 1, 2, 5, 7, 7, 9, 15]
输出:[1, 1, 2, 5, 7, 7, 9, 15]
SORT = insertion_sort()
print(SORT([1,5,9,1,7,2,15,7]))        #使用示范

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值