import math
def parent(i):
return math.floor(i/2)
def left(i):
return 2 * i
def right(i):
return 2 * i + 1
def max_heapify(A, i):
l = left(i)
r = right(i)
largest = i
if l < A.__len__() and A[largest] < A[l]:
largest = l
if r < A.__len__() and A[largest] < A[r]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
print(A)
T = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
max_heapify(T, 1)
import math
def parent(i):
return math.floor(i/2)
def left(i):
return 2 * i
def right(i):
return 2 * i + 1
def max_heapify(A, i):
l = left(i)
r = right(i)
largest = i
if l < A.__len__() and A[largest] < A[l]:
largest = l
if r < A.__len__() and A[largest] < A[r]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
def build_max_heap(A):
for i in range(math.floor(A.__len__()/2), 1, -1):
max_heapify(A, i)
return A
T = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
print(build_max_heap(T))
import math
def parent(i):
return math.floor(i/2)
def left(i):
return 2 * i
def right(i):
return 2 * i + 1
def max_heapify(A, i):
l = left(i)
r = right(i)
largest = i
if l < A.__len__() and A[largest] < A[l]:
largest = l
if r < A.__len__() and A[largest] < A[r]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
def build_max_heap(A):
for i in range(math.floor(A.__len__()/2), 1, -1):
max_heapify(A, i)
return A
def heapsort(A):
A = build_max_heap(A)
S = [] //用以存储排好序的数列
for i in range(A.__len__()-1, 0, -1):
A[1], A[i] = A[i], A[1]
S.append(A[-1])
A = A[0: -1]
max_heapify(A, 1)
return S
T = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
print(heapsort(T))
import math
def parent(i):
return math.floor(i/2)
def left(i):
return 2 * i
def right(i):
return 2 * i + 1
def max_heapify(A, i):
l = left(i)
r = right(i)
largest = i
if l < A.__len__() and A[largest] < A[l]:
largest = l
if r < A.__len__() and A[largest] < A[r]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
def build_max_heap(A):
for i in range(math.floor(A.__len__()/2), 1, -1):
max_heapify(A, i)
return A
def heap_extract_max(A):
if A.__len__() < 1:
print('error')
return 0
max = A[1]
A[1] = A[-1]
A = A[0: -1]
max_heapify(A, 1)
return max, A
T = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
print(heap_extract_max(T))
import math
def parent(i):
return math.floor(i/2)
def left(i):
return 2 * i
def right(i):
return 2 * i + 1
def max_heapify(A, i):
l = left(i)
r = right(i)
largest = i
if l < A.__len__() and A[largest] < A[l]:
largest = l
if r < A.__len__() and A[largest] < A[r]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
def build_max_heap(A):
for i in range(math.floor(A.__len__()/2), 1, -1):
max_heapify(A, i)
return A
def heap_increase_key(A, i, key):
if key < A[i]:
print('error')
return 0
A[i] = key
while i > 1 and A[parent(i)] < A[i]:
A[parent(i)], A[i] = A[i], A[parent(i)]
i = parent(i)
return A
T = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
print(heap_increase_key(T, 7, 23))
import math
def parent(i):
return math.floor(i/2)
def left(i):
return 2 * i
def right(i):
return 2 * i + 1
def max_heapify(A, i):
l = left(i)
r = right(i)
largest = i
if l < A.__len__() and A[largest] < A[l]:
largest = l
if r < A.__len__() and A[largest] < A[r]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
max_heapify(A, largest)
def build_max_heap(A):
for i in range(math.floor(A.__len__()/2), 1, -1):
max_heapify(A, i)
return A
def heap_increase_key(A, i, key):
if key < A[i]:
print('error')
return 0
A[i] = key
while i > 1 and A[parent(i)] < A[i]:
A[parent(i)], A[i] = A[i], A[parent(i)]
i = parent(i)
return A
def max_heap_insert(A, key):
A.append(float('-Inf'))
return heap_increase_key(A, A.__len__()-1, key)
T = [0, 16, 4, 10, 14, 7, 9, 3, 2, 8, 1]
print(max_heap_insert(T, 23))