• python 快速排序-代码示例


    def quick_sort(alist, first, last):
        if first >= last:
            # 如果开始等于结尾,即就一个元素
            return
        mid_value = alist[first]
        low = first
        high = last
        # 对于相等的情况都放到low的位置,所以第一个条件是>=.数据尽量放在一边。
        while low < high:
            # hight开始移动,左移所以-1
            while low < high and alist[high] >= mid_value:
                high -= 1  # high游标左走
            alist[low] = alist[high]  # 把大于mid的值放到low的位置
            # low开始移动
            while low < high and alist[low] < mid_value:
                low += 1
            alist[high] = alist[low]
            # high -= 1  # high游标左走
            # 当low=high时从循环退出
            alist[low] = mid_value
            # 或者 
            # alist[high] = mid_value
    
            # 对low左边的列表排序
            quick_sort(alist, first, low - 1)
            # 对low右边的列表排序
            quick_sort(alist, low + 1, last)
    
    
    if __name__ == '__main__':
        arr = [1, 2, 3, 94, 5, 6, 6, 6, 6]
        quick_sort(arr, 0, len(arr) - 1)
        print(arr)
    
    
  • 相关阅读:
    队列分类梳理
    停止线程
    Docker和Kubernetes
    Future、Callback、Promise
    Static、Final、static final
    线程池梳理
    TCP四次挥手
    http1.0、http1.x、http 2和https梳理
    重排序
    java内存模型梳理
  • 原文地址:https://www.cnblogs.com/c-x-a/p/10984804.html
Copyright © 2020-2023  润新知