python数据结构之quick_sort

时间:2023-01-28 16:02:29

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.

python数据结构之quick_sort

代码如下:

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的更多相关文章

  1. python数据结构与算法

    最近忙着准备各种笔试的东西,主要看什么数据结构啊,算法啦,balahbalah啊,以前一直就没看过这些,就挑了本简单的<啊哈算法>入门,不过里面的数据结构和算法都是用C语言写的,而自己对p ...

  2. python数据结构与算法——链表

    具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...

  3. python数据结构之图的实现

    python数据结构之图的实现,官方有一篇文章介绍,http://www.python.org/doc/essays/graphs.html 下面简要的介绍下: 比如有这么一张图: A -> B ...

  4. Python数据结构与算法--List和Dictionaries

    Lists 当实现 list 的数据结构的时候Python 的设计者有很多的选择. 每一个选择都有可能影响着 list 操作执行的快慢. 当然他们也试图优化一些不常见的操作. 但是当权衡的时候,它们还 ...

  5. Python数据结构与算法--算法分析

    在计算机科学中,算法分析(Analysis of algorithm)是分析执行一个给定算法需要消耗的计算资源数量(例如计算时间,存储器使用等)的过程.算法的效率或复杂度在理论上表示为一个函数.其定义 ...

  6. Python数据结构与循环语句

    # Python数据结构与循环语句:   首先编程是一项技能,类似跑步,期初不必在意细节,能使用起来就行,等学的游刃有余了再回过头来关注细节问题也不迟.  关于买书: 学会python之后,才需要买书 ...

  7. python数据结构之栈与队列

    python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...

  8. python数据结构之树和二叉树&lpar;先序遍历、中序遍历和后序遍历&rpar;

    python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...

  9. Python数据结构之四——set(集合)

    Python版本:3.6.2  操作系统:Windows  作者:SmallWZQ 经过几天的回顾和学习,我终于把Python 3.x中的基础知识介绍好啦.下面将要继续什么呢?让我想想先~~~嗯,还是 ...

随机推荐

  1. XAML中的特殊符号几空白字符处理

    阅读目录 介绍 详细 处理 Demo下载 介绍 XAML标记语言是基于xml的,所以很多xml中的特殊符号在XAML也是需要处理的. 详细 (取自msdn) 字符 Entity 注释 &(“a ...

  2. &lbrack;转&rsqb;Oracle版本号解释

    注意: 在oracle 9.2 版本之后, oracle 的maintenance release number 是在第二数字位更改. 而在之前,是在第三个数字位. 1. Major Database ...

  3. 在Linq to Entity 中使用lambda表达式来实现Left Join和Join

    1.读取用户和部门两个表的左连接: var sg = db.Users.GroupJoin(db.Departments, u => u.DepartmentId, d => d.Depa ...

  4. Webpack&plus;React&plus;ES6入门指南&lbrack;转&rsqb;

    React无疑是今年最火的前端框架,github上的star直逼30,000,基于React的React Native的star也直逼20,000.有了React,组件化似乎不再步履蹒跚,有了Reac ...

  5. Hao123这个流氓

    Author:KillerLegend Date:2014.2.27 From:http://www.cnblogs.com/killerlegend/p/3572591.html Hao123真让人 ...

  6. Oracle基础(四) 用户管理

    一.用户 当创建一个数据实例时,Oracle会创建一些默认的数据库用户,如SYS,SYSTEM和SCOTT等用户.SYS和SYSTEM用户都是ORACLE的系统用户.而Scott用户是Oracle数据 ...

  7. C语言实现定积分求解方法

    求定积分的方法有很多种,下面是我总结的几种比较常用的方法. #include <stdio.h> #include <stdlib.h> #include <math.h ...

  8. Mina - 模拟同步请求

    这篇博客主要就铺代码吧,Mina的一些基础知识可以参考: http://www.cnblogs.com/huangfox/p/3458272.html 场景假设: 1.客户端发送用户信息,服务端根据用 ...

  9. Redis---ZipList&lpar;压缩列表&rpar;

    1.概述 压缩列表是一块连续的内存空间,元素之间紧挨着存储,没有任何冗余空间. Redis 为了节约内存空间使用,zset 和 hash 容器对象在元素个数较少的时候,采用压缩列表 (ziplist) ...

  10. tomcat之日志切割

    日志分割 场景:日志量比较大,且研发程序没有设置分卷 1.配置样例: 文件路径:/etc/logrotate.d/tomcat /data/logs/catalina.out { daily comp ...