#coding=utf-8
import random
ls = [random.randint(1,20) for i in range(30)]
def quick_sort(li):
    if len(li) <=10:
        li.sort()
        return li
    lenth = len(li)
    if li[0]>li[lenth / 2]:
        li[lenth / 2], li[0] = li[0], li[lenth / 2]
    if li[0] > li[lenth-1]:
        li[lenth-1], li[0] = li[0], li[lenth-1]
    if li[lenth/2]>li[lenth-1]:
        li[lenth-1], li[lenth/2] = li[lenth/2], li[lenth-1]
    li[lenth/2], li[lenth-1] = li[lenth-1], li[lenth/2]
    i,j = 0 ,lenth-2
    while 1:
        while li[i]<=li[lenth-1] and i<lenth-2:
            i = i+1
        while li[j] > li[lenth-1] and j >= i:
            j = j-1
        if i>=j:
            break
        else:
            li[i], li[j] = li[j], li[i]
    li[i+1], li[lenth-1] = li[lenth-1], li[i+1]
    li = quick_sort(li[:i]) + quick_sort(li[i:])
    return li
if __name__ == '__main__':
    print quick_sort(ls)
                            
                                    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
二分排序,给你php版的,你用python照着改:
我不是很喜欢可以用递归的强行用while循环来写,要换成循环就干脆换成迭代的。
英文必应搜索quick sort python给的样例: