def heapSort(alist): """堆排序 O(nlogn)""" heap = BinHeap(len(alist)) heap.buildHeap(alist) sort = [] for i in range(len(alist), 0, -1): sort.append(heap.delMin()) return sort
def heap_sort(my_list): sortlst = [] # create a list for sorted values heap = BinHeap() # create a heap heap.buildHeap(my_list) # build heap from an unsorted list while not heap.isEmpty(): # stop the loop when heap is empty sortlst.append( heap.delMin() ) # keep deleting the min value in the heap and reorganizing the heap and append the min value to sortlst return sortlst # return the sorted list
def heapSort(myList): # Create an empty heap minHeap = BinHeap() # Add all list items to minHeap minHeap.buildHeap(myList) # delMin heap items back to list in sorted order size = len(myList) for index in range(0, size): myList[index] = (minHeap.delMin())