python-列表排序

这篇博客探讨了如何使用Python对包含三个整数x, y, z的列表进行升序排序,介绍了包括直接排序、冒泡排序、选择排序、插入排序和希尔排序在内的多种方法。" 78619369,7389451,使用for循环绘制房屋图案,"['Java', 'for循环', '编程基础', '图形打印']
摘要由CSDN通过智能技术生成
输入三个整数x, y, z,形成一个列表,请把这n个数由小到大输出

这就是要求升序排列,用之前写过几种方法

法一

#数组
li=[1,3,5,7,2,0]

# 用sort()排序
li_sort = sorted(li, reverse = False)
print('用sort方法,排列结果:{}'.format(li_sort))

法二:冒泡排序

# 冒泡排序
def bubbleSort(m):
    m = m.copy()
    for time in range(1, len(m)):
        for index in range(len(m) - time):
            if m[index] > m[index+1]:
                m[index], m[index+1] = m[index+1] , m[index]
    return  m

print(bubbleSort(li))

法三:选择排序


# 选择排序
def selectSort(m):
    m = m.copy()
    for seat_L in range(len(m)-1):
        for seat_R in range(seat_L+1, len(m)):
            if m[seat_L] > m[seat_R]:
                m[seat_L], m[seat_R] = m[seat_R], m[seat_L]
    return m

print(selectSort(li))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值