码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序-Python实现

时间:2018-11-13 00:16:55      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:list   代码   左右   取出   and   dex   quick   等于   python实现   

1)、 算法描述:

(1)先从数列中取出一个数作为基准数。

(2)分区过程,将比这个数大的数全放到它的右边,小于或等于它的数全放到它的左边。

(3)再对左右区间重复第二步,直到各区间只有一个数。

2)代码:

def sub_sort(list1, low, height):
    key = list1[low]
    while low < height:
        while low < height and list1[height] >= key:
            height -= 1
        while low < height and list1[height] < key:
            list1[low] = list1[height]
            low += 1
            list1[height] = list1[low]
    list1[low] = key
    return low


def quick_sort(list1, low, height):
    if low < height:
        index_key = sub_sort(list1, low, height)
        quick_sort(list1, low, index_key)
        quick_sort(list1, index_key+1, height)


if __name__ == ‘__main__‘:
    list1 = [8, 10, 9, 6, 4, 16, 5, 13, 26, 18, 2, 45, 34, 23, 1, 7, 3]
    print(list1)
    quick_sort(list1, 0, len(list1) - 1)
    print(list1)

快速排序-Python实现

标签:list   代码   左右   取出   and   dex   quick   等于   python实现   

原文地址:https://www.cnblogs.com/wangchunli-blogs/p/9949572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!