示例#1
0
def extract_max(array):
    max = array[0]
    array[0] = array[len(array) - 1]
    array.pop(len(array) - 1)
    max_heapify(array, 0)
    print(array)
    return max
示例#2
0
def heap_sort(heap):
    build_max_heap(heap)
    heap_size = len(heap) - 1
    for i in range(heap_size, 1, -1):
        heap[1], heap[i] = heap[i], heap[1]
        heap_size -= 1
        max_heapify(heap, heap_size, 1)
示例#3
0
def build_max_heap(array):
	index = (len(array) // 2)
	# starting index should be the last index which nodes are not leaves
	lst = list(range(index))
	lst.reverse()
	for i in lst:
		max_heapify(array,i)
	return array
示例#4
0
def heap_sort(array):
    array = build_max_heap(array)
    sorted_array = []
    # Loop till array has 1 element
    for i in range(len(array) - 1, 0, -1):
        array = swap(i, 0, array)
        maxnum = array[len(array) - 1]
        array.pop(len(array) - 1)
        sorted_array.insert(0, maxnum)
        max_heapify(array, 0)
    # Insert last element of heap
    sorted_array.insert(0, array[0])
    return sorted_array
def extract_max(heap, heap_size):
    max_item = heap[1]
    heap[1] = heap[heap_size]
    heap_size -= 1
    max_heapify(heap, heap_size, 1)
    return max_item
示例#6
0
def build_max_heap(heap):
    heap_size = len(heap) - 1
    for i in range(heap_size // 2, 0, -1):
        max_heapify(heap, heap_size, i)
def build_max_heap(A):
    A_heap_size = len(A)
    for i in range(1, A_heap_size // 2 + 1)[::-1]:
        max_heapify(A, i)