堆排序
1 data=[50, 16, 30, 10, 60, 90, 2, 80, 70] 2 #构建大顶堆 3 def heap_adjust(L,start,end): 4 tmp=L[start] 5 i=start 6 j=2*i 7 while j<=end: 8 if j<end and L[j]<L[j+1]: 9 j+=1 10 if tmp<L[j]: 11 L[i]=L[j] 12 i=j 13 j=2*i 14 else: 15 break 16 L[i]=tmp 17 18 #构建堆排序 19 def heap_sort(data): 20 #构建大顶堆的下标,大顶堆的下标从1开始 21 L=[0]+data 22 #计算数组的长度 23 length_L=len(data) 24 #构建大顶堆 25 start_count=length_L//2 26 i=0 27 while i<start_count: 28 heap_adjust(L,start_count-i,length_L) 29 i+=1 30 i=0 31 while length_L-i>1: 32 j=length_L-i 33 L[1],L[j]=L[j],L[1] 34 heap_adjust(L,1,j-1) 35 i+=1 36 return L[1:] 37 print(heap_sort(data))