1 def quick_sort(alist,start,end):
2 #递归退出的条件
3 if start >= end:
4 return
5 #基准数
6 mid = alist[start]
7 low = start
8 high = end
9 while low < high:
10 #从右向左
11 while low < high and alist[high] > mid:
12 high -= 1
13 #将high索引对应的元素赋值给low
14 alist[low] = alist[high]
15 #从左往右
16 while low < high and alist[low] < mid:
17 low += 1
18 alist[high] = alist[low]
19 #将基准数放置到对应位置
20 alist[low] = mid
21 #比基准数小的左边的数 重复调用quick_soort()
22 quick_sort(alist,start,low-1)
23 ##比基准数大的右边的数 重复调用quick_soort()
24 quick_sort(alist,low+1,end)
25 if __name__ == '__main__':
26 alist = [54,26,93,17,77,31,44,55,20]
27 print('原数组:',alist)
28 quick_sort(alist,0,len(alist)-1)
29 print('现数组:',alist)
1 原数组: [54, 26, 93, 17, 77, 31, 44, 55, 20]
2 现数组: [17, 20, 26, 31, 44, 54, 55, 77, 93]