Skip to content

Commit

Permalink
Merge pull request #461 from williebonavente/trash-talk
Browse files Browse the repository at this point in the history
Update HeapSort
  • Loading branch information
williebonavente authored Oct 9, 2023
2 parents 754562f + 8b04e13 commit ac0714b
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 ac0714b

Please sign in to comment.