|
@@ -8,25 +8,10 @@ Al final se imprimen los promedios de cada algortimo
|
8
|
8
|
"""
|
9
|
9
|
from random import randint
|
10
|
10
|
import time
|
11
|
|
-from merge import merge
|
12
|
11
|
|
13
|
|
-# Python program for implementation of MergeSort
|
14
|
|
-
|
15
|
|
-# Merges two subarrays of arr[].
|
16
|
|
-# First subarray is arr[l..m]
|
17
|
|
-# Second subarray is arr[m+1..r]
|
18
|
|
-
|
19
|
|
-def mergeSort(lista, l, r):
|
20
|
|
- if l < r:
|
21
|
|
-
|
22
|
|
- # Same as (l+r)//2, but avoids overflow for
|
23
|
|
- # large l and h
|
24
|
|
- m = l+(r-l)//2
|
25
|
|
-
|
26
|
|
- # Sort first and second halves
|
27
|
|
- mergeSort(lista, l, m)
|
28
|
|
- mergeSort(lista, m+1, r)
|
29
|
|
- merge(lista, l, m, r)
|
|
12
|
+def mergeSort(lista):
|
|
13
|
+ #definan el algoritmo de ordenamiento mergesort
|
|
14
|
+ return lista
|
30
|
15
|
|
31
|
16
|
def heapSort(lista):
|
32
|
17
|
#definan el algoritmo de ordenamiento heapsort
|
|
@@ -41,8 +26,8 @@ def shellSort(lista):
|
41
|
26
|
return lista
|
42
|
27
|
|
43
|
28
|
maxValor=1000 #define el valor maximo de los elementos de la lista
|
44
|
|
-largoLista=10 #define el largo de las listas a ordenar
|
45
|
|
-veces=1 #define las veces que se va a hacer el ordenamiento
|
|
29
|
+largoLista=1000 #define el largo de las listas a ordenar
|
|
30
|
+veces=100 #define las veces que se va a hacer el ordenamiento
|
46
|
31
|
|
47
|
32
|
acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
|
48
|
33
|
acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
|
|
@@ -55,30 +40,26 @@ for i in range(veces):
|
55
|
40
|
quicklista=list(mergelista)
|
56
|
41
|
searchlista=list(mergelista)
|
57
|
42
|
|
58
|
|
- t1 = time.process_time() #tomamos el tiempo inicial
|
59
|
|
- mergeSort(mergelista,0,len(mergelista)-1) #ejecutamos el algoritmo mergeSort
|
60
|
|
- acumulaMerge+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
|
61
|
|
- print(mergelista) #desplegamos la lista
|
62
|
|
-
|
63
|
|
- t1 = time.process_time() #tomamos el tiempo inicial
|
64
|
|
- heapSort(heaplista) #ejecutamos el algoritmo heapSort
|
65
|
|
- acumulaHeap+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
|
66
|
|
- print(heaplista) #desplegamos la lista
|
67
|
|
-
|
68
|
|
- t1 = time.process_time() #tomamos el tiempo inicial
|
69
|
|
- quickSort(quicklista) #ejecutamos el algoritmo quickSort
|
70
|
|
- acumulaQuick+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
|
71
|
|
- print(quicklista) #desplegamos la lista
|
72
|
|
-
|
73
|
|
- t1 = time.process_time() #tomamos el tiempo inicial
|
74
|
|
- shellSort(searchlista) #ejecutamos el algoritmo shellSort
|
75
|
|
- acumulaShell+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
|
76
|
|
- print(searchlista) #desplegamos la lista
|
|
43
|
+ t1 = time.clock() #seteamos el tiempo al empezar
|
|
44
|
+ mergeSort(mergelista) #ejecutamos el algoritmo mergeSort
|
|
45
|
+ acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
46
|
+
|
|
47
|
+ t1 = time.clock() #seteamos el tiempo al empezar
|
|
48
|
+ heapSort(heaplista) #ejecutamos el algoritmo heapSort
|
|
49
|
+ acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
50
|
+
|
|
51
|
+ t1 = time.clock() #seteamos el tiempo al empezar
|
|
52
|
+ quickSort(quicklista) #ejecutamos el algoritmo quickSort
|
|
53
|
+ acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
54
|
+
|
|
55
|
+ t1 = time.clock() #seteamos el tiempo al empezar
|
|
56
|
+ shellSort(searchlista) #ejecutamos el algoritmo shellSort
|
|
57
|
+ acumulaShell+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
77
|
58
|
|
78
|
59
|
#imprimos los resultados
|
79
|
|
-print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
|
80
|
|
-print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
|
81
|
|
-print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
|
82
|
|
-print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
|
83
|
|
-print ("ShellSort " + str(acumulaShell/veces) + " segundos")
|
|
60
|
+print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
|
|
61
|
+print "MergeSort " + str(acumulaMerge/veces) + " segundos"
|
|
62
|
+print "HeapSort " + str(acumulaHeap/veces) + " segundos"
|
|
63
|
+print "QuickSort " + str(acumulaQuick/veces) + " segundos"
|
|
64
|
+print "ShellSort " + str(acumulaShell/veces) + " segundos"
|
84
|
65
|
|