正文
博客地址:https://www.cnblogs.com/zylyehuo/
# _*_coding:utf-8_*_
def partition(li, left, right):
tmp = li[left]
while left < right:
while left < right and li[right] >= tmp: # 从右面找比tmp小的数
right -= 1 # 往左走一步
li[left] = li[right] # 把右边的值写到左边空位上
print(li, 'right')
while left < right and li[left] <= tmp:
left += 1
li[right] = li[left] # 把左边的值写到右边空位上
print(li, 'left')
li[left] = tmp # 把tmp归位
return left
def _quick_sort(li, left, right):
if left < right: # 至少两个元素
mid = partition(li, left, right)
_quick_sort(li, left, mid - 1)
_quick_sort(li, mid + 1, right)
def quick_sort(li):
_quick_sort(li, 0, len(li) - 1)
ls = [5, 7, 9, 4, 8, 3, 6, 1]
quick_sort(ls)
print(ls)