mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Update heap_sort.py
This commit is contained in:
parent
4002609137
commit
3d8b77bf80
@ -3,25 +3,26 @@
|
|||||||
# author: bt3gl
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
def heap_sort(self, lst: List[int]) -> None:
|
def heap_sort(self, array) -> list:
|
||||||
|
|
||||||
def max_heapify(heap_size, index):
|
def max_heapify(heap_size, index):
|
||||||
|
|
||||||
left, right = 2 * index + 1, 2 * index + 2
|
left, right = 2 * index + 1, 2 * index + 2
|
||||||
largest = index
|
largest = index
|
||||||
|
|
||||||
if left < heap_size and lst[left] > lst[largest]:
|
if left < heap_size and array[left] > array[largest]:
|
||||||
largest = left
|
largest = left
|
||||||
if right < heap_size and lst[right] > lst[largest]:
|
elif if right < heap_size and array[right] > array[largest]:
|
||||||
largest = right
|
largest = right
|
||||||
if largest != index:
|
elif largest != index:
|
||||||
lst[index], lst[largest] = lst[largest], lst[index]
|
array[index], array[largest] = array[largest], array[index]
|
||||||
max_heapify(heap_size, largest)
|
max_heapify(heap_size, largest)
|
||||||
|
|
||||||
for i in range(len(lst) // 2 - 1, -1, -1):
|
for i in range(len(lst) // 2 - 1, -1, -1):
|
||||||
max_heapify(len(lst), i)
|
max_heapify(len(array), i)
|
||||||
|
|
||||||
for i in range(len(lst) - 1, 0, -1):
|
for i in range(len(array) - 1, 0, -1):
|
||||||
lst[i], lst[0] = lst[0], lst[i]
|
array[i], array[0] = array[0], array[i]
|
||||||
max_heapify(i, 0)
|
max_heapify(i, 0)
|
||||||
|
|
||||||
|
return array
|
||||||
|
Loading…
x
Reference in New Issue
Block a user