Skip to content

Commit

Permalink
Update HeapSort
Browse files Browse the repository at this point in the history
  • Loading branch information
williebonavente committed Oct 9, 2023
1 parent 12c0f4d commit 8b04e13
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cantSolve/HeapSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,25 @@ def heap_sort(array):

for i in range(len(array) - 1, 0, -1):
array[0], array[i] = array[i], array[0]
max_heapify(array, 0, i)
max_heapify(array, 0, i)


def build_max_heap(array):
for i in range(len(array) // 2 - 1, -1, -1):
max_heapify(array, i, len(array))


def max_heapify(array, i, heap_size):
largest = i
left = 2 * i + 1
right = 2 * i + 2

if left < heap_size and array[left] > array[largest]:
largest = left

if right < heap_size and array[right] > array[largest]:
largest = right

if largest != i:
array[i], array[largest] = array[largest], array[i]
max_heapify(array, largest, heap_size)

0 comments on commit 8b04e13

Please sign in to comment.