|
@@ -21,14 +21,54 @@ def mergeSort(lista):
|
21
|
21
|
#definan el algoritmo de ordenamiento mergesort
|
22
|
22
|
return lista
|
23
|
23
|
|
|
24
|
+
|
|
25
|
+# Tomada de: https://www.geeksforgeeks.org/heap-sort/
|
|
26
|
+# To heapify subtree rooted at index i.
|
|
27
|
+# n is size of heap
|
|
28
|
+def heapify(arr, n, i):
|
|
29
|
+ largest = i # Initialize largest as root
|
|
30
|
+ l = 2 * i + 1 # left = 2*i + 1
|
|
31
|
+ r = 2 * i + 2 # right = 2*i + 2
|
|
32
|
+
|
|
33
|
+ # See if left child of root exists and is
|
|
34
|
+ # greater than root
|
|
35
|
+ if l < n and arr[largest] < arr[l]:
|
|
36
|
+ largest = l
|
|
37
|
+
|
|
38
|
+ # See if right child of root exists and is
|
|
39
|
+ # greater than root
|
|
40
|
+ if r < n and arr[largest] < arr[r]:
|
|
41
|
+ largest = r
|
|
42
|
+
|
|
43
|
+ # Change root, if needed
|
|
44
|
+ if largest != i:
|
|
45
|
+ arr[i], arr[largest] = arr[largest], arr[i] # swap
|
|
46
|
+
|
|
47
|
+ # Heapify the root.
|
|
48
|
+ heapify(arr, n, largest)
|
|
49
|
+
|
|
50
|
+# Tomada de: https://www.geeksforgeeks.org/heap-sort/
|
24
|
51
|
def heapSort(lista):
|
25
|
52
|
#definan el algoritmo de ordenamiento heapsort
|
|
53
|
+ n = len(lista)
|
|
54
|
+
|
|
55
|
+ # Build a maxheap.
|
|
56
|
+ for i in range(n//2 - 1, -1, -1):
|
|
57
|
+ heapify(lista, n, i)
|
|
58
|
+
|
|
59
|
+ # One by one extract elements
|
|
60
|
+ for i in range(n-1, 0, -1):
|
|
61
|
+ lista[i], lista[0] = lista[0], lista[i] # swap
|
|
62
|
+ heapify(lista, i, 0)
|
|
63
|
+
|
26
|
64
|
return lista
|
27
|
65
|
|
|
66
|
+
|
28
|
67
|
def quickSort(lista):
|
29
|
68
|
#definan el algoritmo de ordenamiento quicksort
|
30
|
69
|
return lista
|
31
|
70
|
|
|
71
|
+
|
32
|
72
|
def shellSort(lista):
|
33
|
73
|
#definan el algoritmo de ordenamiento shellsort
|
34
|
74
|
|
|
@@ -98,6 +138,7 @@ def shellSort(lista):
|
98
|
138
|
|
99
|
139
|
return lista
|
100
|
140
|
|
|
141
|
+
|
101
|
142
|
maxValor=1000 #define el valor maximo de los elementos de la lista
|
102
|
143
|
largoLista=1000 #define el largo de las listas a ordenar
|
103
|
144
|
veces=100 #define las veces que se va a hacer el ordenamiento
|
|
@@ -110,26 +151,26 @@ acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
|
110
|
151
|
for i in range(veces):
|
111
|
152
|
lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
|
112
|
153
|
|
113
|
|
- t1 = time.clock() #seteamos el tiempo al empezar
|
114
|
|
- mergeSort(lista) #ejecutamos el algoritmo mergeSort
|
115
|
|
- acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
154
|
+ t1 = time.process_time() #seteamos el tiempo al empezar
|
|
155
|
+ mergeSort(lista) #ejecutamos el algoritmo mergeSort
|
|
156
|
+ acumulaMerge+=time.process_time()-t1 #acumulamos el tiempo de ejecucion
|
116
|
157
|
|
117
|
|
- t1 = time.clock() #seteamos el tiempo al empezar
|
118
|
|
- heapSort(lista) #ejecutamos el algoritmo heapSort
|
119
|
|
- acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
158
|
+ t1 = time.process_time() #seteamos el tiempo al empezar
|
|
159
|
+ heapSort(lista) #ejecutamos el algoritmo heapSort
|
|
160
|
+ acumulaHeap+=time.process_time()-t1 #acumulamos el tiempo de ejecucion
|
120
|
161
|
|
121
|
|
- t1 = time.clock() #seteamos el tiempo al empezar
|
122
|
|
- quickSort(lista) #ejecutamos el algoritmo quickSort
|
123
|
|
- acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
162
|
+ t1 = time.process_time() #seteamos el tiempo al empezar
|
|
163
|
+ quickSort(lista) #ejecutamos el algoritmo quickSort
|
|
164
|
+ acumulaQuick+=time.process_time()-t1 #acumulamos el tiempo de ejecucion
|
124
|
165
|
|
125
|
|
- t1 = time.clock() #seteamos el tiempo al empezar
|
126
|
|
- shellSort(lista) #ejecutamos el algoritmo shellSort
|
127
|
|
- acumulaShell+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
166
|
+ t1 = time.process_time() #seteamos el tiempo al empezar
|
|
167
|
+ shellSort(lista) #ejecutamos el algoritmo shellSort
|
|
168
|
+ acumulaShell+=time.process_time()-t1 #acumulamos el tiempo de ejecucion
|
128
|
169
|
|
129
|
170
|
#imprimos los resultados
|
130
|
|
-print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
|
131
|
|
-print "MergeSort " + str(acumulaMerge/veces) + " segundos"
|
132
|
|
-print "HeapSort " + str(acumulaHeap/veces) + " segundos"
|
133
|
|
-print "QuickSort " + str(acumulaQuick/veces) + " segundos"
|
134
|
|
-print "ShellSort " + str(acumulaShell/veces) + " segundos"
|
|
171
|
+print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
|
|
172
|
+print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
|
|
173
|
+print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
|
|
174
|
+print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
|
|
175
|
+print ("ShellSort " + str(acumulaShell/veces) + " segundos")
|
135
|
176
|
|