defpartition(list, left, right):
temp =list[left]while left < right:while left < right andlist[right]>= temp:
right -=1list[left]=list[right]while left < right andlist[left]<= temp:
left +=1list[right]=list[left]list[left]= temp
return left
defquick_sort(list, left, right):if left < right:
index = partition(list, left, right)
quick_sort(list, left, index-1)
quick_sort(list, index+1, right)list=[8,5,4,3,7,2,9]
quick_sort(list,0,len(list)-1)print(list)
快速排序取列表的一个元素值作为flag,将数组划分为两个部分。flag左边的值都比它小,右边的值都比它大。递归完成。平均时间复杂度:O(n log(n))最坏时间复杂度:O(n2n^2n2)def partition(list, left, right): temp = list[left] while left < right: while left < right and list[right] >= temp: ri