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

  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数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)

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

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

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

随机推荐

  1. python --------------网络(socket)编程

    一.网络协议 客户端/服务器架构 1.硬件C/S架构(打印机) 2.软件C/S架构(互联网中处处是C/S架构):B/S架构也是C/S架构的一种,B/S是浏览器/服务器 C/S架构与socket的关系: ...

  2. 两种lca的求法:树上倍增,tarjan

    第一种:树上倍增 f[x,k]表示x的2^k辈祖先,即x向根结点走2^k步达到的结点. 初始条件:f[x][0]=fa[x] 递推式:f[x][k]=f[ f[x][k-1] ][k-1] 一次bfs ...

  3. centos7.5上一步步部署jumpserver

    1.基础环境:centos7.5:关闭防火墙和selinux 2.修改字符集,否则可能会报input/output error 的问题,因为日志里打印了中文 [root@xzw ~]# localed ...

  4. Ubuntu强制重启后提示emergency mode

    起因 win10+Ubuntu16.04双系统,在ubuntu下训练一个卷积网但是显存拙计卡死了,于是手贱强制按下电源开关重启. 现象 重启后从grub进ubuntu,并不进图形化的登录界面,而是提示 ...

  5. Task任务的屏障机制

    Barrier 是 .Net 提供的一直并发的机制,它允许多个任务同步他们不同阶段的并发工作. 这里的关键点是[多个任务]和[不同阶段]. 假设有4个相同的任务(Task),每个任务都有4个阶段(Ph ...

  6. 微信公众平台开发教程Java版(六) 事件处理(菜单点击/关注/取消关注)

    https://blog.csdn.net/tuposky/article/details/40589325

  7. Ncurses-窗口

    前面介绍过标准屏幕 stdscr, stdscr 只是 WINDOW 结构的一个特例. 我们可以使用函数 newwin 和 delwin 来创建和销毁窗口 WINDOW *newwin(int num ...

  8. Java第三阶段学习(七、线程池、多线程)

    一.线程池 1.概念: 线程池,其实就是一个容纳多个线程的容器,其中的线程可以重复使用,省去了频繁创建线程对象的过程,无需反复创建线程而消耗过多资源,是JDK1.5以后出现的. 2.使用线程池的方式- ...

  9. IO流-file

    1.1 IO概述 回想之前写过的程序,数据都是在内存中,一旦程序运行结束,这些数据都没有了,等下次再想使用这些数据,可是已经没有了.那怎么办呢?能不能把运算完的数据都保存下来,下次程序启动的时候,再把 ...

  10. 【CF526F】Pudding Monsters

    题意: 给你一个排列pi,问你有对少个区间的值域段是连续的. n≤3e5 题解: bzoj3745