|
@ -33,7 +33,7 @@ def get_right(i): |
|
|
def repair_heap(vals_list, root, arr_top): |
|
|
def repair_heap(vals_list, root, arr_top): |
|
|
""" |
|
|
""" |
|
|
Sifts the root element of a heap to the correct position, to |
|
|
Sifts the root element of a heap to the correct position, to |
|
|
correct a max heap. This assumes the children of the root node are max heaps. |
|
|
|
|
|
|
|
|
correct a max heap. This assumes the children of the root/ node are max heaps. |
|
|
|
|
|
|
|
|
:param vals_list: list of values, which represents a heap structure. |
|
|
:param vals_list: list of values, which represents a heap structure. |
|
|
:param root: the index of the node we're working from/ using as a root. |
|
|
:param root: the index of the node we're working from/ using as a root. |
|
@ -65,6 +65,7 @@ def repair_heap(vals_list, root, arr_top): |
|
|
root = swap |
|
|
root = swap |
|
|
left = get_left(root) |
|
|
left = get_left(root) |
|
|
right = get_right(root) |
|
|
right = get_right(root) |
|
|
|
|
|
|
|
|
return vals_list |
|
|
return vals_list |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -118,4 +119,4 @@ if __name__ == "__main__": |
|
|
vals_list = [randint(0, (2**16)) for i in range(list_len)] |
|
|
vals_list = [randint(0, (2**16)) for i in range(list_len)] |
|
|
heap_sorted = heapsort(list(vals_list)) |
|
|
heap_sorted = heapsort(list(vals_list)) |
|
|
py_sorted = sorted(vals_list) |
|
|
py_sorted = sorted(vals_list) |
|
|
print("Did the sort work? {}".format(heap_sorted == py_sorted)) |
|
|
|
|
|
|
|
|
print("Did the sort work? {}".format(heap_sorted == py_sorted)) |