python数据结构之quick_sort
Quick sort , also known as partition-exchange sort, divides the data to be sorted into two separate parts by a single sort, in which all the data of one part is smaller than all the other parts. Then, according to this method, the two parts of the data are sorted separately, and the whole sorting process can be recursively, so that the entire data becomes an ordered sequence. The steps are: 1. Pick an element from the series, called "pivot", 2. Reorder the series, all elements are placed in front of the reference smaller than the reference value, and all elements are placed larger than the reference value. Behind the benchmark (the same number can go to either side). After the end of this partition, the benchmark is in the middle of the sequence. This is called a partition operation. 3. Recursively sorts sub-columns that are smaller than the reference value element and sub-columns that are larger than the reference value element. The bottommost case of recursion is that the size of the series is zero or one, that is, it has always been sorted. Although it has been recursively, this algorithm will always end, because in each iteration, it will at least put an element to its final position.

代码如下:
def quick_sort(alist,start,end):
if start >= end:
return mid = alist[start]
#设置初始元素
low = start
high = end
while low < high :
#判断是否排序完成
# 后面元素左移
while low < high and alist[high] >= mid:
high -=1
alist[low]=alist[high]
#后面元素左移
# 前面元素右移
while low < high and alist[low]< mid:
low +=1
alist[high]=alist[low]
alist[low]=mid
#分开排序
quick_sort(alist,start,low-1)
quick_sort(alist,low+1,end)
# 执行函数打印
alist = [54,26,93,17,77,31,44,55,20]
quick_sort(alist,0,len(alist)-1)
print(alist)
python数据结构之quick_sort的更多相关文章
- python数据结构与算法
最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- python数据结构之图的实现
python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...
- Python数据结构与算法--List和Dictionaries
Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...
- Python数据结构与算法--算法分析
在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...
- Python数据结构与循环语句
# Python数据结构与循环语句: 首先编程是一项技能,类似跑步,期初不必在意细节,能使用起来就行,等学的游刃有余了再回过头来关注细节问题也不迟. 关于买书: 学会python之后,才需要买书 ...
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...
- python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)
python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...
- Python数据结构之四——set(集合)
Python版本:3.6.2 操作系统:Windows 作者:SmallWZQ 经过几天的回顾和学习,我终于把Python 3.x中的基础知识介绍好啦.下面将要继续什么呢?让我想想先~~~嗯,还是 ...
随机推荐
- Confluence 6 关于统一插件管理器
所有的组件通过 统一插件管理器(Universal Plugin Manager)进行管理,这个也被称为 UPM.UPM 可以在几乎所有的 Atlassian 应用中找到,能够提供完整同意的插件安装管 ...
- bat如何提取文本指定行的内容
背景:使用CTS框架运行完测试后,会在logs中生成devices_log和host_log,在results中生成相应的结果(报告).根据报告信息我们可以得知失败的用例,但是却不能知道为什么用例会失 ...
- Dubbo常用配置文件分析及核心源码阅读(SPI.Extension)
1.多版本支持: 基于上篇博客的 快速启动 Dubbo 服务 的代码进行多版本支持的演示:基于原来的实现类GpHelloImpl ,我们需要新增一个新版本的实类:GpHelloImpl2 public ...
- 【linux】centos6.9通过virtualenv安装python3.5
参考:http://www.linuxidc.com/Linux/2015-08/121352.htm wget https://www.python.org/ftp/python/3.5.4/Pyt ...
- 《剑指offer》 调整数组顺序使得奇数在偶数前面
本题来自<剑指offer> 调整数组顺序使得奇数在偶数前面 题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分, ...
- Java 并发类
java.util.concurrent包里 提供了一批线程安全的类 一. java.util.concurrent.atomic java.util.concurrent.atomic包里的原子处理 ...
- git 注意事项
1,用户凭证 github的两种url地址 http ssh :由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两 ...
- 用来表达更复杂的sql语句!!!!!extra 原声sql
extra 用来表达更复杂的sql语句!!!!! extra可以指定一个或多个 参数,例如 select, where or tables. 这些参数都不是必须的,但是你至少要使用一个!要注意这些额外 ...
- Eclipse中java文件生成jar文件的方法
在eclpse中找到你要导出的java程序 选中它 单击文件 -->export 在弹出的export对话框中找到 jar File 单击选中-->next 按图示顺序依次 选 ...
- json如果不在pom中添加依赖会抛出500异常
所以必须添加以下依赖: <!--jackson相关依赖--><!-- https://mvnrepository.com/artifact/com.fasterxml.jackson ...