赞
踩
def patition(li, left, right):
count = li[left] #record the special number
#it is the first number, usually
while left < right:
while left < right and li[right] >= count: #find the number who is less than count
#it must judge left < right
#instead, right will minus 1 one more time
right -= 1
li[left] = li[right] #exchange left and right
while left < right and li[left] <= count: #find the number who is greater than count
left += 1
li[right] = li[left]
li[left] = count
return left
def quick_sort(li, left, right): #recursion
if left < right:
mid = patition(li, left, right) #compute the middle number
quick_sort(li, left, mid - 1) #the left recursion
quick_sort(li, mid + 1, right) #the right recursion
print(li)
li = [6,3,4,9,8,1,2,5,6]
quick_sort(li, 0, len(li)-1)
写英文注释是为了锻炼自己的英语。
为大家推荐一个数据结构与算法的视频:https://www.bilibili.com/video/BV1mp4y1D7UP?p=17
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。