Browse Source

Added Mergesort algorithm

AngelRomero5 1 year ago
parent
commit
2aea5dde9f
1 changed files with 38 additions and 5 deletions
  1. 38
    5
      sorting.py

+ 38
- 5
sorting.py View File

9
 from random import randint
9
 from random import randint
10
 import time
10
 import time
11
 from copy import deepcopy
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
 def heapSort(lista):
48
 def heapSort(lista):
18
 	#definan el algoritmo de ordenamiento heapsort
49
 	#definan el algoritmo de ordenamiento heapsort
35
 	return (duration, res)
66
 	return (duration, res)
36
 
67
 
37
 maxValor = 1000 	#define el valor maximo de los elementos de la lista
68
 maxValor = 1000 	#define el valor maximo de los elementos de la lista
38
-largoLista = 1000 #define el largo de las listas a ordenar
69
+largoLista = 1000   #define el largo de las listas a ordenar
39
 veces = 100 		#define las veces que se va a hacer el ordenamiento
70
 veces = 100 		#define las veces que se va a hacer el ordenamiento
40
 
71
 
41
 acumulaMerge = 0 	#variable para acumular el tiempo de ejecucion del mergesort
72
 acumulaMerge = 0 	#variable para acumular el tiempo de ejecucion del mergesort
49
 	quicklista = deepcopy(mergelista)
80
 	quicklista = deepcopy(mergelista)
50
 	searchlista = deepcopy(mergelista)
81
 	searchlista = deepcopy(mergelista)
51
 
82
 
52
-	acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
83
+	with recursion_depth(1500): # This function excedes python's recursion limit
84
+		acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
85
+
53
 	acumulaHeap += timeCode(lambda: heapSort(heaplista))[0]
86
 	acumulaHeap += timeCode(lambda: heapSort(heaplista))[0]
54
 	acumulaQuick += timeCode(lambda: quickSort(quicklista))[0]
87
 	acumulaQuick += timeCode(lambda: quickSort(quicklista))[0]
55
 	acumulaShell += timeCode(lambda: shellSort(searchlista))[0]
88
 	acumulaShell += timeCode(lambda: shellSort(searchlista))[0]