最大值堆排序算法
def MaxHeap(heap,heapsize,i):#获取最大值堆核心算法
left=2*i+1
right=2*i+2
sign=i
if left:
sign=left
if right:
sign=right
if i!=sign:
heap[i],heap[sign]=heap[sign],heap[i]
MaxHeap(heap,heapsize,sign)
def BuildHeap(heap):#获取最大值堆
heapsize=len(heap)-1
for i in xrange((heapsize//2),-1,-1):
MaxHeap(heap,heapsize,i)
def HeapSort(heap):#最大值堆排序
BuildHeap(heap)
print heap
for i in xrange(len(heap)-1,-1,-1):
heap[0],heap[i]=heap[i],heap[0]
MaxHeap(heap,i,0)
return heap
if __name__=="__main__":
heap=[100,1,100,1,100,1,100,1,100,1,2,3,2,1,2,1,4,3,2,2,4,21,1,100,1,2,1,223,13,212,11,11]
print HeapSort(heap)