#!/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)
快速排序(Quick Sort)
最新推荐文章于 2021-09-06 14:21:11 发布