• 双路快排


    序列元素重复,是序列已经排好序的一种特殊情况,如果一个序列中的元素全部相同,也将出现最差情况。
    如果序列中存在大量重复元素,在普通快排中,相等的元素会被全部放到分区点的一边,这样会大大增加快排的时间复杂度,双路快排就是用来解决这个问题的。
    能够将序列均衡分开的分区点才是好的分区点。均匀分开意味着保持O(logn)的复杂度。

    from random import shuffle, randrange
    
    def quick_sort(lst, left, right):
        # 当只有一个元素的时候退出递归
        if left < right:
            p = partition(lst, left, right)
            # 左右分区分别递归
            quick_sort(lst, left, p)
            quick_sort(lst, p+1, right)
    
    def partition(lst, left, right):
        rand = randrange(left, right)
        lst[left], lst[rand] = lst[rand], lst[left]   # 随机挑选出一个元素,与第一个元素交换,作为分区点
        pivot = lst[left]    # 以第一个元素为分区点
        leftpos = left + 1
        rightpos = right - 1
        while leftpos <= rightpos:
            while leftpos <= rightpos and lst[leftpos] <= pivot:
                leftpos += 1
            while leftpos <= rightpos and lst[rightpos] >= pivot:
                rightpos -= 1
            if leftpos > rightpos:
                break
            lst[leftpos], lst[rightpos] = lst[rightpos], lst[leftpos]
        # 将pivot放入分区点
        lst[leftpos-1], lst[left] = lst[left], lst[leftpos-1]
        # 返回分区点索引
        return leftpos-1
    
    
    source = list(range(10))
    shuffle(source)
    print(source)
    
    quick_sort(source, 0, len(source))
    print(source)
    # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
  • 相关阅读:
    css 中 overflow: hidden清楚浮动的真正原因
    Get、Post请求中文乱码问题有效解决方法
    安装PostgreSQL和卸载PostgreSQL
    PostgresSql日期字段比较大小
    关于多线程编程的一点思考
    Python 虚拟环境 pyenv、venv(pyvenv)、virtualenv之间的区别
    python常用命令
    python中各种文件打开模式a w r
    python-安装
    day1-安装配置
  • 原文地址:https://www.cnblogs.com/keithtt/p/10177179.html
Copyright © 2020-2023  润新知