快速排序

 1 def partition(start,end,array):
 2     pivot=array[start]
 3     left,right=start+1,end
 4 
 5     while True:
 6 
 7         while left<=right and  array[left]<=pivot:
 8             left+=1
 9         while left <=right and  pivot<=array[right]:
10             right-=1
11         if left<=right:
12             print(left,right,pivot,array[left],array[right])
13             array[left],array[right]=array[right],array[left]
14         else:
15             break
16     array[start],array[right] = array[right],array[start]
17 
18     return right
19 
20 def quick_sort(array,start,end):
21     if start>=end: return
22 
23     p = partition(start,end,array)
24     quick_sort(array,start,p-1)
25     quick_sort(array,p+1,end)
26 
27 array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44]
28 quick_sort(array,0,len(array)-1)
29 print(array)

 

posted @ 2021-03-15 09:23  ChevisZhang  阅读(51)  评论(0编辑  收藏  举报