python实现快速排序
来源:互联网 发布:java shell 回写 编辑:程序博客网 时间:2024/05/16 05:47
快速排序使用分而治之来获得与归并排序相同的优点,而不使用额外的存储。然而,作为权衡,有可能列表不能被分成两半。当这种情况发生时,我们将看到性能降低。
快速排序首先选择一个值,该值称为 枢轴值。虽然有很多不同的方法来选择枢轴值,我们将使用列表中的第一项。枢轴值的作用是帮助拆分列表。枢轴值属于最终排序列表(通常称为拆分点)的实际位置,将用于将列表划分为快速排序的后续调用。
def quickSort(alist): quickSortHelper(alist,0,len(alist)-1)def quickSortHelper(alist,first,last): if first<last: splitpoint = partition(alist,first,last) quickSortHelper(alist,first,splitpoint-1) quickSortHelper(alist,splitpoint+1,last)def partition(alist,first,last): pivotvalue = alist[first] leftmark = first+1 rightmark = last done = False while not done: while leftmark <= rightmark and alist[leftmark] <= pivotvalue: leftmark = leftmark + 1 while alist[rightmark] >= pivotvalue and rightmark >= leftmark: rightmark = rightmark -1 if rightmark < leftmark: done = True else: temp = alist[leftmark] alist[leftmark] = alist[rightmark] alist[rightmark] = temp temp = alist[first] alist[first] = alist[rightmark] alist[rightmark] = temp return rightmarkalist = [54,26,93,17,77,31,44,55,20]quickSort(alist)
时间复杂度
最优时间复杂度:O(nlogn)
最坏时间复杂度:O(n2)
稳定性:不稳定
阅读全文
0 0
- 快速排序Python实现
- python实现快速排序
- Python实现快速排序
- python 实现快速排序
- Python实现快速排序
- 快速排序Python实现
- python实现快速排序
- Python实现快速排序
- python实现快速排序
- python 实现快速排序
- Python实现快速排序
- python实现快速排序
- Python实现快速排序
- python实现快速排序
- Python实现快速排序
- Python实现快速排序
- python实现快速排序
- 快速排序的python实现
- 2017年10月26日训练笔记
- 43. Multiply Strings
- 求助!!!
- 事件监听的三种方法
- Java基本信息配置
- python实现快速排序
- uboot笔记2
- Windows 10 资源管理器黑色风格
- Gateway教程——如何查看Gateway服务地址以及端口号
- 删除文件,磁盘空间没变
- lnmp 原理
- Android自定义View圆形数据分析器
- Python语言学习之三:sys.argv[]
- Tomcat集群搭建超详细