import random #先换一个 def getPosition(array, left, right): # 一、先求中,中右换,左为板 mid = (left+right)//2 p = array[mid] array[mid] = array[right] array[right] = p board = left # 二、再遍历,若小p,i板换,板加一 for i in range(left, right): #遍历到right-1 if array[i] < p: array[i], array[board] = array[board], array[i] board += 1 # 三、后右板,返回板 array[right], array[board] = array[board], array[right] return board # 再换全部 def quickSort(l, left, right): # 左小右,求p点。p减一,递归右,p加一,递归左。 if left < right: p = getPosition(l, left, right) quickSort(l, left, p-1) quickSort(l, p+1, right) # main n = 10 l = [] for i in range(1, n+1): l.append(random.randint(1, 101)) print(l) quickSort(l, 0, n-1) print(l)
原文:https://www.cnblogs.com/SlowIsFast/p/12960871.html