【python】快排的三种实现

def quick_sort(x, low, high):
    if len(x) <= 1 or low >= high:
        return x
    base, l, r = x[low], low, high
    while l < r:
        while l < r and x[r] >= base:
            r -= 1
        x[l] = x[r]
        while l < r and x[l] <= base:
            l += 1
        x[r] = x[l]
    x[l] = base
    quick_sort(x, low, l-1)
    quick_sort(x, l+1, high)
    return x
def qsort(x):
    if not x:
        return []
    base = x[0]
    less = [ele for ele in x[1:] if ele < base]
    more = [ele for ele in x[1:] if ele >= base]
    return qsort(less) + [base] + qsort(more)
def quick_sort(x, low, high):
    if len(x) <= 1 or low >= high:
        return
    stack = [low, high]
    while stack:
        l, r = stack.pop(0), stack.pop(0)
        if l >= r:
            continue
        i = l - 1
        for j in range(l, r):
            if x[j] < x[r]:
                i += 1
                x[i], x[j] = x[j], x[i]
        x[i + 1], x[r] = x[r], x[i + 1]
        stack.extend([l, i, i + 2, r])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值