iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python数据结构之quick_sor
  • 405
分享到

python数据结构之quick_sor

数据结构pythonquick_sor 2023-01-30 22:01:27 405人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

Quick sort , also known as partition-exchange sort, divides the data to be sorted into two separate parts by a single s

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.

image

代码如下:

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)

--结束END--

本文标题: python数据结构之quick_sor

本文链接: https://www.lsjlt.com/news/179753.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python数据结构之quick_sor
    Quick sort , also known as partition-exchange sort, divides the data to be sorted into two separate parts by a single s...
    99+
    2023-01-30
    数据结构 python quick_sor
  • python数据结构之 set
     在数学概念中,被意为整合元素的定义区域在python中,set最大的作用是用来去重 set常见操作:In [158]: s ={1,1,1,1,2,22,33,3,3,3} In [159]: sOut[159]: {1,2, 3, 22...
    99+
    2023-01-31
    数据结构 python set
  • python数据结构之链表
    '''' 链表的实现,单向链表 ''' '''建立节点''' class jd:     def __init__(self,data):         self.data = data         self.next = None...
    99+
    2023-01-31
    数据结构 链表 python
  • 五--python之数据结构(Data
    1、列表list:a=[value1,value2,value3,value4,…]方法论methods:list.append(x)  #列表追加,等同于a[len(a):] = [x]list.extend(L)   #列表加长,等同于...
    99+
    2023-01-31
    数据结构 python Data
  • python数据结构之链表(linked
    目录 基础 知识 1.1 链表的基本结构 1.2 节点类和链表节点的定义 1.3 顺序打印和逆序打印 链表的基本操作 2.1 计算链表长度 2.2 从前,后插入数据 2.3 查找与删除 参考 1.基础 知识 1....
    99+
    2023-01-31
    数据结构 链表 python
  • Python数据结构之栈详解
    目录0. 学习目标1. 栈的基本概念1.1 栈的基本概念1.2 栈抽象数据类型1.3 栈的应用场景2. 栈的实现2.1 顺序栈的实现2.1.1 栈的初始化2.2 链栈的实现2.3 栈...
    99+
    2024-04-02
  • Python数据结构之图的存储结构详解
    一、图的定义 图是一种比树更复杂的一种数据结构,在图结构中,结点之间的关系是任意的,任意两个元素之间都可能相关,因此,它的应用极广。图中的数据元素通常被称为顶点 ( V e r t ...
    99+
    2024-04-02
  • Python学习之day3数据结构之列表
                                                          数据结构之列表一、列表定义      列表是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目。列表中的项目应包括在...
    99+
    2023-01-31
    数据结构 列表 Python
  • python数据结构之希尔排序
    def shell_sort(alist): n=len(alist) gap= int(n / 2) #步长 while gap>0: for i in range(gap,n): ...
    99+
    2023-01-30
    希尔 数据结构 python
  • Python数据结构之队列详解
    目录0. 学习目标1. 队列的基本概念1.1 队列的基本概念1.2 队列抽象数据类型1.3 队列的应用场景2. 队列的实现2.1 顺序队列的实现2.2 链队列的实现2.3 队列的不同...
    99+
    2024-04-02
  • 用Python实现数据结构之栈
    栈是最简单的数据结构,也是最重要的数据结构。它的原则就是后进先出(LIFO),栈被使用于非常多的地方,例如浏览器中的后退按钮,文本编辑器中的撤销机制,接下来我们用Python来具体实现这个数据结构。 栈中的方法 作为一个栈(用S来表示...
    99+
    2023-01-30
    数据结构 Python
  • Python基础之数据结构详解
    目录一、列表1.1 列表更新元素1.2 列表增加元素1.3 列表删除元素1.4 列表的其他操作二、元组2.1 删除元组2.2 元组的其他操作三、字典3.1 字典删除元素3.2 字典的...
    99+
    2024-04-02
  • Python数据结构之链表详解
    目录0.学习目标1.线性表的链式存储结构1.1指针相关概念1.2指针结构1.3结点1.4结点类2.单链表的实现2.1单链表的初始化2.2获取单链表长度2.3读取指定位置元素2.4查找...
    99+
    2024-04-02
  • python数据结构之面向对象
    目录1. 面向对象编程2. 构建类3. 继承3.1 继承案例前文学习: python数据结构:数据类型.python数据结构输入输出及控制和异常. 今天我们来学习面向对象编程,面向对...
    99+
    2024-04-02
  • python数据结构之搜索讲解
    目录1. 普通搜索2. 顺序搜索1.1 无序下的顺序查找1.2 有序下的顺序查找2.二分查找3.散列查找3.1 几种散列函数3.2 处理散列表冲突3.3 散列表的实现(加1重复)4....
    99+
    2024-04-02
  • python数据结构之选择排序
    选择排序(select_sort)是一个基础排序,它主要通过查找已给序列中的元素的最大或者最小元素,然后将其放在序列的起始位置或者结束位置,并通过多次这样的循环完成对已知序列的排序,在我们对n个元素进行操作时,我们至少需要n-1次。 de...
    99+
    2023-01-30
    数据结构 python
  • 用Python实现数据结构之树
    树是由根结点和若干颗子树构成的。树是由一个集合以及在该集合上定义的一种关系构成的。集合中的元素称为树的结点,所定义的关系称为父子关系。父子关系在树的结点之间建立了一个层次结构。在这种层次结构中有一个结点具有特殊的地位,这个结点称为该树...
    99+
    2023-01-30
    数据结构 之树 Python
  • python 数据结构
    list(列表)创建list方式1  : 直接创建  theList = [1,2,3,4,5,6,7,8,9]                    ==> [1,2,3,4,5,6,7,8,9]方式2 : 使用内建方法list()...
    99+
    2023-01-31
    数据结构 python
  • python数据结构
    一:数据结构  数据结构可以认为他们是用来处理一些数据的或者说是存储数据。  对于数据结构的介绍会关系到类和对象的定义,此处对这两个定义加以描述。  何为类:说道类首先我们能够想到类型,在数据结构中类型有哪些常用的类型有int整型,floa...
    99+
    2023-01-31
    数据结构 python
  • 用Python实现数据结构之链表
    链表与栈,队列不一样,它是由一个个节点构成的,每个节点存储着本身的一些信息,也存储着其他一个或多个节点的引用,可以从一个节点找到其他的节点,节点与节点之间就像是有链连在一起一样,这种数据结构就叫做链表 单向链表是链表的最简单形式,链表...
    99+
    2023-01-30
    数据结构 链表 Python
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作