前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python数据结构之quick_sor

python数据结构之quick_sor

作者头像
py3study
发布2020-01-19 14:26:14
5260
发布2020-01-19 14:26:14
举报
文章被收录于专栏:python3

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.

代码如下:

代码语言:javascript
复制
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)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/03/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档