zoukankan      html  css  js  c++  java
  • 快速排序-Python实现

    1)、 算法描述:

    (1)先从数列中取出一个数作为基准数。

    (2)分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。

    (3)再对左右区间重复第二步,直到各区间只有一个数。

    2)代码:

    def sub_sort(list1, low, height):
        key = list1[low]
        while low < height:
            while low < height and list1[height] >= key:
                height -= 1
            while low < height and list1[height] < key:
                list1[low] = list1[height]
                low += 1
                list1[height] = list1[low]
        list1[low] = key
        return low


    def quick_sort(list1, low, height):
        if low < height:
            index_key = sub_sort(list1, low, height)
            quick_sort(list1, low, index_key)
            quick_sort(list1, index_key+1, height)


    if __name__ == '__main__':
        list1 = [8, 10, 9, 6, 4, 16, 5, 13, 26, 18, 2, 45, 34, 23, 1, 7, 3]
        print(list1)
        quick_sort(list1, 0, len(list1) - 1)
        print(list1)

  • 相关阅读:
    Nodejs
    webpack与gulp的区别
    gulpjs
    Commonjs、AMD、CMD
    建造者模式
    工厂模式
    设计模式分类
    python的接口
    Python代码教你批量将PDF转为Word
    什么是“堆”,"栈","堆栈","队列",它们的区别?
  • 原文地址:https://www.cnblogs.com/wangchunli-blogs/p/9949572.html
Copyright © 2011-2022 走看看