low冒泡

    import random
    
    
    def sort(t, p=1):
        if not isinstance(p, int):
            # 抛出异常
            # return 强制中断,不往下执行
            return TypeError("order的类型错误")
    
        # 参数3 step 步长, 默认为1
        for i in range(len(t) - 1, 0,-1):
            for m in range(0, i):
                if p == 1:
                    if t[m] > t[m + 1]:
                        t[m], t[m + 1] = t[m + 1], t[m]
                else:
                    if t[m] < t[m + 1]:
                        t[m], t[m + 1] = t[m + 1], t[m]
        return t
    
    
    list = []
    for i in range(52):
        a = random.randint(-200, 200)
        list.append(a)
        
print('原列表',list)
f = sort(list,-1)
print('排序后',f)

猜你喜欢

转载自blog.csdn.net/weixin_42590877/article/details/88599350