美文网首页
Python实现快速排序

Python实现快速排序

作者: 小学弟_ | 来源:发表于2018-07-16 18:20 被阅读0次

    from randomimport Random

    def quick_sort(arry):

    if len(arry) >1:

    qsort(arry, 0, len(arry) -1)

    def qsort(arry, start, end):

    base = arry[start]

    l = start

    r = end

    while l < r:

    while l < rand arry[r] >= base:

    r -=1

            if l == r:

    break

            else:

    arry[l], arry[r] = arry[r], arry[l]

    while l < rand arry[l] <= base:

    l +=1

            if l == r:

    break

            else:

    arry[l], arry[r] = arry[r], arry[l]

    # now l == r

        if l -1 > start:

    qsort(arry, start, l -1)

    if r +1 < end:

    qsort(arry, r +1, end)

    r = Random()

    a = []

    for iin range(10):

    a.append(r.randint(0, 100))

    print(a)

    quick_sort(a)

    print(a)

    相关文章

      网友评论

          本文标题:Python实现快速排序

          本文链接:https://www.haomeiwen.com/subject/loobpftx.html