python随笔-冒泡排序算法

# 冒泡排序算法
# 第一步,将每一组的进行比对,数字大的放在后面,
# 第二部,重复第一步的步骤,一直到最后一组的比对完成为止
def maopao():
    temp = [56, 54, 3243, 323, 3, 82, 121, 33, 4, 1]
    for j in range(1,len(temp)):
        found = False
        for i in range(len(temp)-j):
            if(temp[i]>temp[i+1]):  #将满足条件的数组的值进行替换
                lists = temp[i]
                temp[i] = temp[i+1]
                temp[i+1] = lists
            else:
                found = True     # 做的优化,当所有相邻的数组内的数值均满足条件时,已经排序完成,不需要继续比对了
        if not found:
            break
    print(temp)

# 知识点,range(3) 为数组0,1,2    range(1,3) 为数组 1,2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值