|
@@ -9,41 +9,10 @@ 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:]))
|
46
|
12
|
|
|
13
|
+def mergeSort(lista):
|
|
14
|
+ #definan el algoritmo de ordenamiento mergesort
|
|
15
|
+ return lista
|
47
|
16
|
|
48
|
17
|
def heapSort(lista):
|
49
|
18
|
#definan el algoritmo de ordenamiento heapsort
|
|
@@ -81,7 +50,6 @@ def shellSort(lst):
|
81
|
50
|
|
82
|
51
|
return lst
|
83
|
52
|
|
84
|
|
-
|
85
|
53
|
# timeCode function/thunk -> (duration, return value)
|
86
|
54
|
# measures the time it takes for a function/thunk to return
|
87
|
55
|
def timeCode(fn):
|
|
@@ -91,7 +59,7 @@ def timeCode(fn):
|
91
|
59
|
return (duration, res)
|
92
|
60
|
|
93
|
61
|
maxValor = 1000 #define el valor maximo de los elementos de la lista
|
94
|
|
-largoLista = 1000 #define el largo de las listas a ordenar
|
|
62
|
+largoLista = 1000 #define el largo de las listas a ordenar
|
95
|
63
|
veces = 100 #define las veces que se va a hacer el ordenamiento
|
96
|
64
|
|
97
|
65
|
acumulaMerge = 0 #variable para acumular el tiempo de ejecucion del mergesort
|
|
@@ -105,9 +73,7 @@ for i in range(veces):
|
105
|
73
|
quicklista = deepcopy(mergelista)
|
106
|
74
|
searchlista = deepcopy(mergelista)
|
107
|
75
|
|
108
|
|
- with recursion_depth(1100): # This function excedes python's recursion limit
|
109
|
|
- acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
|
110
|
|
-
|
|
76
|
+ acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
|
111
|
77
|
acumulaHeap += timeCode(lambda: heapSort(heaplista))[0]
|
112
|
78
|
acumulaQuick += timeCode(lambda: quickSort(quicklista))[0]
|
113
|
79
|
acumulaShell += timeCode(lambda: shellSort(searchlista))[0]
|