|
@@ -19,10 +19,69 @@ Al final se imprimen los promedios de cada algortimo
|
19
|
19
|
from random import randint
|
20
|
20
|
import time
|
21
|
21
|
|
|
22
|
+
|
|
23
|
+#===============================
|
|
24
|
+#Modificación a código: Diego
|
|
25
|
+#Añado función heapify
|
|
26
|
+#===============================
|
|
27
|
+def heapify(listaHeap, largoLista, i):
|
|
28
|
+ largest = i
|
|
29
|
+ left = 2 * i + 1
|
|
30
|
+ right = 2 * i + 2
|
|
31
|
+
|
|
32
|
+ if left < largoLista and listaHeap[i] < listaHeap[left]:
|
|
33
|
+ largest = left
|
|
34
|
+
|
|
35
|
+ if right < largoLista and listaHeap[largest] < listaHeap[right]:
|
|
36
|
+ largest = right
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+def merge(lista, p, q, r):
|
|
40
|
+ left = lista[p:q+1] # copy the left subarray
|
|
41
|
+ right = lista[q+1:r+1] # copy the right subarray
|
|
42
|
+ i = 0 # index for the left subarray
|
|
43
|
+ j = 0 # index for the right subarray
|
|
44
|
+ k = p # index for the sorted list
|
|
45
|
+
|
|
46
|
+ # Keep adding to the sorted list, while both lists have elements
|
|
47
|
+ while i < len(left) and j < len(right):
|
|
48
|
+ if left[i] <= right[j]:
|
|
49
|
+ lista[k] = left[i]
|
|
50
|
+ i += 1
|
|
51
|
+ else:
|
|
52
|
+ lista[k] = right[j]
|
|
53
|
+ j += 1
|
|
54
|
+
|
|
55
|
+ k += 1
|
|
56
|
+
|
|
57
|
+ # If right finished first, then fill up the rest with the left subarray
|
|
58
|
+ while i < len(left):
|
|
59
|
+ lista[k] = left[i]
|
|
60
|
+ i += 1
|
|
61
|
+ k += 1
|
|
62
|
+
|
|
63
|
+ # If left finished first, then fill up the rest with the right subarray
|
|
64
|
+ while j < len(right):
|
|
65
|
+ lista[k] = right[j]
|
|
66
|
+ j += 1
|
|
67
|
+ k += 1
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+def mergeSortAux(lista, p, r):
|
|
71
|
+ # If array has one element or less, return
|
|
72
|
+ if p >= r:
|
|
73
|
+ return
|
|
74
|
+ # Else, split the array in half
|
|
75
|
+ q = int((p+r)/2) # find the middle
|
|
76
|
+ mergeSortAux(lista, p, q) # Sort the left subarray
|
|
77
|
+ mergeSortAux(lista, q+1, r) # Sort the right subarray
|
|
78
|
+ merge(lista, p, q, r) # Combine both subarrays
|
|
79
|
+
|
|
80
|
+
|
22
|
81
|
def mergeSort(listaMerge):
|
23
|
82
|
#definan el algoritmo de ordenamiento mergesort
|
24
|
|
-
|
25
|
|
- return lista
|
|
83
|
+ mergeSortAux(listaMerge, 0, len(listaMerge)-1)
|
|
84
|
+ return listaMerge
|
26
|
85
|
|
27
|
86
|
#===============================
|
28
|
87
|
#Modificación a código: Diego
|