# -*- coding: utf-8 -*-
# author: zhonghua
# filename: sort_quick.py
# create: 2016/3/28
# version: 1.0
# 快速排序
def quick(lst, left, right):
if left >= right:
return
key = lst[left]
low = left
high = right
# 大循环
while low < high:
# 必须是lst[high] >= key, 否则left和right会停在一个位置重复循环
while low < high and lst[high] >= key:
high -= 1
lst[low] = lst[high]
# 这里不能是lst[low] <= key, 否则会出现low超出lst范围的情况
while low < high and lst[low] < key:
low += 1
lst[high] = lst[low]
lst[low] = key
# 递归
quick(lst, left, low)
quick(lst, low + 1, right)
if __name__ == '__main__':
lst = [19, 28, 30, 5, 8, 60, 72, 94, 68, 28]
quick(lst, 0, len(lst)-1)
for i in lst:
print i
网友评论