Selaa lähdekoodia

Merge remote-tracking branch 'origin/sortingDayan' into sortingFinal

José C.S. Curet 1 vuosi sitten
vanhempi
commit
752d6b2b5e
1 muutettua tiedostoa jossa 62 lisäystä ja 17 poistoa
  1. 62
    17
      sorting.py

+ 62
- 17
sorting.py Näytä tiedosto

@@ -11,8 +11,24 @@ import time
11 11
 import math
12 12
 
13 13
 def mergeSort(lista):
14
-	#definan el algoritmo de ordenamiento mergesort
15
-	return lista
14
+        if (len(lista) == 1):
15
+            return lista
16
+        
17
+        n = len(lista)
18
+        
19
+        # Se divide la lista
20
+        firstHalf = lista[0:n//2]
21
+        secondHalf = lista[n//2:n]
22
+
23
+        # Se vuelve a llamar para cada mitad
24
+        firstHalf = mergeSort(firstHalf)
25
+        secondHalf = mergeSort(secondHalf)
26
+
27
+        # Se unen las listas
28
+        lista = merge(firstHalf,secondHalf)
29
+
30
+        return lista
31
+
16 32
 
17 33
 #Esta es la implementación de Heap sort de Geeksforgeeks
18 34
 #https://www.geeksforgeeks.org/heap-sort/
@@ -75,6 +91,33 @@ def heapify(lista, n, i):
75 91
 		#Se llama heapify en la raíz nueva
76 92
 		heapify(lista, n, largest)
77 93
 
94
+def merge(firstHalf, secondHalf):
95
+        mergeList = []
96
+
97
+        # Mientras ninguna lista este vacia
98
+        while (len(firstHalf) != 0) and (len(secondHalf) != 0):
99
+                
100
+                if firstHalf[0] > secondHalf[0]:
101
+                        n = secondHalf.pop(0)
102
+                        mergeList.append(n)
103
+                else:
104
+                        n = firstHalf.pop(0)
105
+                        mergeList.append(n)
106
+
107
+        # Si la primera mitad no esta vacia, se anade a la lista merge
108
+        while len(firstHalf) != 0:
109
+                mergeList.append(firstHalf[0])
110
+                firstHalf.pop(0)
111
+
112
+        # Si la segunda mitad no esta vacia, se anade a la lista merge
113
+        while len(secondHalf) != 0:
114
+                mergeList.append(secondHalf[0])
115
+                secondHalf.pop(0)
116
+
117
+        return mergeList
118
+                        
119
+        
120
+
78 121
 maxValor=1000 	#define el valor maximo de los elementos de la lista
79 122
 largoLista=1000 #define el largo de las listas a ordenar
80 123
 veces=100 		#define las veces que se va a hacer el ordenamiento 
@@ -90,27 +133,29 @@ for i in range(veces):
90 133
 	quicklista=list(mergelista)
91 134
 	searchlista=list(mergelista)
92 135
 
93
-	# t1 = time.clock() 				#seteamos el tiempo al empezar
94
-	# mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
95
-	# acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
136
+	t1 = time.time() 				#seteamos el tiempo al empezar
137
+	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
138
+	acumulaMerge+=time.time()-t1 	#acumulamos el tiempo de ejecucion
96 139
 	
97
-	t1 = time.process_time()						#seteamos el tiempo al empezar
98
-	heapSort(heaplista)						#ejecutamos el algoritmo heapSort
99
-	acumulaHeap += time.process_time() - t1			#acumulamos el tiempo de ejecucion
140
+	t1 = time.time()				#seteamos el tiempo al empezar
141
+	heapSort(heaplista)					#ejecutamos el algoritmo heapSort
142
+	acumulaHeap+=time.time()-t1 	#acumulamos el tiempo de ejecucion
100 143
 	
101
-	# t1 = time.clock()				#seteamos el tiempo al empezar
102
-	# quickSort(quicklista)				#ejecutamos el algoritmo quickSort
103
-	# acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
144
+	t1 = time.time()				#seteamos el tiempo al empezar
145
+	quickSort(quicklista, 0, len(quicklista) - 1)				#ejecutamos el algoritmo quickSort
146
+	acumulaQuick+=time.time()-t1 	#acumulamos el tiempo de ejecucion
104 147
 	
105
-	# t1 = time.clock()				#seteamos el tiempo al empezar
106
-	# shellSort(searchlista)				#ejecutamos el algoritmo shellSort
107
-	# acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
148
+	t1 = time.time()				#seteamos el tiempo al empezar
149
+	shellSort(searchlista)				#ejecutamos el algoritmo shellSort
150
+	acumulaShell+=time.time()-t1 	#acumulamos el tiempo de ejecucion
108 151
 
109 152
 #imprimos los resultados
110 153
 print("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
111
-#print("MergeSort " + str(acumulaMerge/veces) + " segundos")
154
+print("MergeSort " + str(acumulaMerge/veces) + " segundos")
112 155
 print("HeapSort " + str(acumulaHeap/veces) + " segundos")
113
-#print("QuickSort " + str(acumulaQuick/veces) + " segundos")
114
-#print("ShellSort " + str(acumulaShell/veces) + " segundos")
156
+print("QuickSort " + str(acumulaQuick/veces) + " segundos")
157
+print("ShellSort " + str(acumulaShell/veces) + " segundos")
158
+
159
+
115 160
 
116 161