|
@@ -19,11 +19,11 @@ def heapSort(lista):
|
19
|
19
|
Carlos Hernández
|
20
|
20
|
Implementación de heapSort.
|
21
|
21
|
"""
|
22
|
|
- def max_heapify(lista, idx: int, heap_size: int):
|
|
22
|
+ def max_heapify(lista, idx, heap_size):
|
23
|
23
|
"""Convertir el nodo `idx` y sus descendientes en un max heap."""
|
24
|
|
- left_idx: int = 2 * idx + 1
|
25
|
|
- right_idx: int = 2 * idx + 2
|
26
|
|
- largestval_idx: int = idx
|
|
24
|
+ left_idx = 2 * idx + 1
|
|
25
|
+ right_idx = 2 * idx + 2
|
|
26
|
+ largestval_idx = idx
|
27
|
27
|
if left_idx < heap_size and lista[idx] < lista[left_idx]:
|
28
|
28
|
largestval_idx = left_idx
|
29
|
29
|
if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
|
|
@@ -32,12 +32,12 @@ def heapSort(lista):
|
32
|
32
|
lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
|
33
|
33
|
max_heapify(lista, largestval_idx, heap_size)
|
34
|
34
|
|
35
|
|
- def build_max_heap(lista, heap_size: int):
|
|
35
|
+ def build_max_heap(lista, heap_size):
|
36
|
36
|
"""Construir un max heap the un heap dado."""
|
37
|
37
|
for idx in range((heap_size - 1) // 2, -1, -1):
|
38
|
38
|
max_heapify(lista, idx, heap_size)
|
39
|
39
|
|
40
|
|
- heap_size: int = len(lista)
|
|
40
|
+ heap_size = len(lista)
|
41
|
41
|
build_max_heap(lista, heap_size)
|
42
|
42
|
for idx in range(len(lista) - 1, 0, -1):
|
43
|
43
|
lista[0], lista[idx] = lista[idx], lista[0]
|