1 def Partition(list,p,q): 2 #这里是用来分块的算法。 3 x = list[p] 4 i = p 5 for j in range(p+1,q+1): #注意range是顾前不顾后的,所以后面的区间值要大一位 6 if list[j]<x: 7 i+=1 8 list[i],list[j] = list[j],list[i] 9 10 list[p], list[i] = list[i], list[p] 11 return i 12 13 14 def Quicksort(List,left,right): 15 if left<right: 16 r = Partition(List,left,right) 17 Quicksort(List,left,r-1) 18 Quicksort(List,r+1,right) 19 20 21 def main(Out_list): 22 #外部接口 23 Out_list_left =0 24 Out_list_right = len(Out_list)-1 25 Quicksort(Out_list,Out_list_left,Out_list_right) 26 return Out_list 27 28 if __name__ =="__main__": 29 A = [32,34,21,45,666,213,41,23,78,64,36,83,18,41,332,6466,432] 30 A_left = 0 31 A_right = len(A)-1 32 print(A_right) 33 Quicksort(A,A_left,A_right) 34 print(A)
非常简单的算法。