|
|
|
|
19
|
Carlos Hernández
|
19
|
Carlos Hernández
|
20
|
Implementación de heapSort.
|
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
|
"""Convertir el nodo `idx` y sus descendientes en un max heap."""
|
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
|
if left_idx < heap_size and lista[idx] < lista[left_idx]:
|
27
|
if left_idx < heap_size and lista[idx] < lista[left_idx]:
|
28
|
largestval_idx = left_idx
|
28
|
largestval_idx = left_idx
|
29
|
if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
|
29
|
if right_idx < heap_size and lista[largestval_idx] < lista[right_idx]:
|
|
|
|
|
32
|
lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
|
32
|
lista[idx], lista[largestval_idx] = lista[largestval_idx], lista[idx]
|
33
|
max_heapify(lista, largestval_idx, heap_size)
|
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
|
"""Construir un max heap the un heap dado."""
|
36
|
"""Construir un max heap the un heap dado."""
|
37
|
for idx in range((heap_size - 1) // 2, -1, -1):
|
37
|
for idx in range((heap_size - 1) // 2, -1, -1):
|
38
|
max_heapify(lista, idx, heap_size)
|
38
|
max_heapify(lista, idx, heap_size)
|
39
|
|
39
|
|
40
|
- heap_size: int = len(lista)
|
|
|
|
|
40
|
+ heap_size = len(lista)
|
41
|
build_max_heap(lista, heap_size)
|
41
|
build_max_heap(lista, heap_size)
|
42
|
for idx in range(len(lista) - 1, 0, -1):
|
42
|
for idx in range(len(lista) - 1, 0, -1):
|
43
|
lista[0], lista[idx] = lista[idx], lista[0]
|
43
|
lista[0], lista[idx] = lista[idx], lista[0]
|