快速排序(Quick Sort)

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# @Author  : guangxu.qi
# @Time    : 2020/9/15 17:07
# @FileName: 07_quick_sort.py
# @Description:

def quick_sort(alist):
    quick_sort_helper(alist, 0, len(alist) - 1)


def quick_sort_helper(alist, first, last):
    if first < last:
        split_point = partition(alist, first, last)
        quick_sort_helper(alist, first, split_point - 1)
        quick_sort_helper(alist, split_point + 1, last)


def partition(alist, first, last):
    pivot_value = alist[first]

    left_index = first + 1
    right_index = last

    done = False

    while not done:
        while left_index <= right_index and alist[left_index] <= pivot_value:
            left_index = left_index + 1

        while right_index >= left_index and alist[right_index] >= pivot_value:
            right_index = right_index - 1

        if right_index < left_index:
            done = True
        else:
            alist[left_index], alist[right_index] = alist[right_index], alist[left_index]

    alist[first], alist[right_index] = alist[right_index], alist[first]

    return right_index


if __name__ == '__main__':
    alist = [5, 3, 1, 4, 2, 3]
    res = quick_sort(alist)
    print(alist)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值