|
@@ -9,10 +9,41 @@ Al final se imprimen los promedios de cada algortimo
|
9
|
9
|
from random import randint
|
10
|
10
|
import time
|
11
|
11
|
from copy import deepcopy
|
|
12
|
+import sys
|
|
13
|
+
|
|
14
|
+# This function was created to prevent program from stopping before recursion finished
|
|
15
|
+# Changes python's recursion limit
|
|
16
|
+class recursion_depth:
|
|
17
|
+ def __init__(self, limit):
|
|
18
|
+ self.limit = limit
|
|
19
|
+ self.default_limit = sys.getrecursionlimit()
|
|
20
|
+
|
|
21
|
+ def __enter__(self):
|
|
22
|
+ sys.setrecursionlimit(self.limit)
|
|
23
|
+
|
|
24
|
+ def __exit__(self, type, value, traceback):
|
|
25
|
+ sys.setrecursionlimit(self.default_limit)
|
|
26
|
+
|
|
27
|
+# Mergesort algorithm
|
|
28
|
+def mergeSort(lista): # Ángel G. Romero Rosario on 10082022
|
|
29
|
+
|
|
30
|
+ def merge(l1, l2):
|
|
31
|
+ if len(l1) == 0:
|
|
32
|
+ return l2
|
|
33
|
+ elif len(l2) == 0:
|
|
34
|
+ return l1
|
|
35
|
+ elif l1[0] < l2[0]:
|
|
36
|
+ return l1[0:1] + merge(l1[1:],l2) # If l1[0] < l2[0] save l1[0] first to the list and call the function again
|
|
37
|
+ else:
|
|
38
|
+ return l2[0:1] + merge(l1, l2[1:]) # If l2[0] < l1[0] save l2[0] first to the list and call the function again
|
|
39
|
+
|
|
40
|
+ if len(lista) <= 1: # If there are no more items, return lista
|
|
41
|
+ return lista
|
|
42
|
+
|
|
43
|
+ else:
|
|
44
|
+ mid = len(lista) // 2 # Find the middle in lista and call function to merge lista
|
|
45
|
+ return merge(mergeSort(lista[:mid]), mergeSort(lista[mid:]))
|
12
|
46
|
|
13
|
|
-def mergeSort(lista):
|
14
|
|
- #definan el algoritmo de ordenamiento mergesort
|
15
|
|
- return lista
|
16
|
47
|
|
17
|
48
|
def heapSort(lista):
|
18
|
49
|
#definan el algoritmo de ordenamiento heapsort
|
|
@@ -32,7 +63,6 @@ def quickSort(lista):
|
32
|
63
|
x = i
|
33
|
64
|
del lista[lista.index(i)]
|
34
|
65
|
lista.insert(lista.index(p), x)
|
35
|
|
- print(lista)
|
36
|
66
|
|
37
|
67
|
l = quickSort(lista[ :lista.index(p)])
|
38
|
68
|
m = quickSort(lista[lista.index(p) + 1:])
|
|
@@ -59,7 +89,7 @@ def timeCode(fn):
|
59
|
89
|
return (duration, res)
|
60
|
90
|
|
61
|
91
|
maxValor = 1000 #define el valor maximo de los elementos de la lista
|
62
|
|
-largoLista = 1000 #define el largo de las listas a ordenar
|
|
92
|
+largoLista = 1000 #define el largo de las listas a ordenar
|
63
|
93
|
veces = 100 #define las veces que se va a hacer el ordenamiento
|
64
|
94
|
|
65
|
95
|
acumulaMerge = 0 #variable para acumular el tiempo de ejecucion del mergesort
|
|
@@ -73,7 +103,9 @@ for i in range(veces):
|
73
|
103
|
quicklista = deepcopy(mergelista)
|
74
|
104
|
searchlista = deepcopy(mergelista)
|
75
|
105
|
|
76
|
|
- acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
|
|
106
|
+ with recursion_depth(1500): # This function excedes python's recursion limit
|
|
107
|
+ acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
|
|
108
|
+
|
77
|
109
|
acumulaHeap += timeCode(lambda: heapSort(heaplista))[0]
|
78
|
110
|
acumulaQuick += timeCode(lambda: quickSort(quicklista))[0]
|
79
|
111
|
acumulaShell += timeCode(lambda: shellSort(searchlista))[0]
|