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 从站点首页集中访问面板
如果你选择设置一个页面为你的站点主页面,但是你还是希望你的用户能够访问 Confluence 的主面板,你可以将主面板的连接添加到应用导航(Application Navigator)中. 希望添加 ...
- 兼容性很好的纯css圆角
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Winhex数据恢复学习笔记(三)
上次对文件系统进行简单的分析,这次就文件的镜像功能做一介绍 1.首先镜像的概念:镜像就是数据的副本,是原来数据在相同位置上以相同的排列模式生成的拷贝,所以镜像可以用来还原原始数据,代替原始数据工作,镜 ...
- c++实现 给定直角停车位两个点,求取剩余两点坐标。
//2018-09-08-fourmi /*************************include head files************************************ ...
- 用JAVA写一个简单的英文加密器
package qhs; import java.util.Scanner; public class JiaM { public static void main(String[] args) { ...
- CMD批处理——forfiles命令使用,自动删除过期备份文件
公司服务器用来备份数据的硬盘过段时间就会被备份文件占满,弄得我老是要登录到服务器去手工删除那些老的文件,有时忘记了就会导致硬盘空间不足而无法备份.因为只要保留最近几天的备份,如果可以做一个批处理让系统 ...
- 深入理解JSCore
https://blog.csdn.net/MeituanTech/article/details/82108667
- java流程控制语句总结
1.选择结构 if 方式1: 格式: if(条件表达式) { 语句体; } 执行流程: 如果条件表达式值为true, 执行语句体 如果条件表达式值为false,不执行语句体 方式2: 格式: if(条 ...
- Python_collections_namedtuple可命名元组
namedtuple:用来构建带字段名的元组 import collections # 创建类,两种创建方法 MytupleClass = collections.namedtuple('Mytupl ...
- Codeforces 1132E (看题解)
感觉这个题挺有意思的, 我们可以将 L = lcm(1, 2, 3, ... , 8) 看作一组. 然后用dp[ i ][ j ]表示到第 i 种物品当前的值为 j 能用L的最大数量. #includ ...