Browse Source

Merge sort

Dayan 1 year ago
parent
commit
3b800da269
1 changed files with 64 additions and 17 deletions
  1. 64
    17
      sorting.py

+ 64
- 17
sorting.py View File

@@ -10,8 +10,24 @@ from random import randint
10 10
 import time
11 11
 
12 12
 def mergeSort(lista):
13
-	#definan el algoritmo de ordenamiento mergesort
14
-	return lista
13
+        if (len(lista) == 1):
14
+            return lista
15
+        
16
+        n = len(lista)
17
+        
18
+        # Se divide la lista
19
+        firstHalf = lista[0:n//2]
20
+        secondHalf = lista[n//2:n]
21
+
22
+        # Se vuelve a llamar para cada mitad
23
+        firstHalf = mergeSort(firstHalf)
24
+        secondHalf = mergeSort(secondHalf)
25
+
26
+        # Se unen las listas
27
+        lista = merge(firstHalf,secondHalf)
28
+
29
+        return lista
30
+
15 31
 
16 32
 def heapSort(lista):
17 33
 	#definan el algoritmo de ordenamiento heapsort
@@ -25,6 +41,33 @@ def shellSort(lista):
25 41
 	#definan el algoritmo de ordenamiento shellsort
26 42
 	return lista
27 43
 
44
+def merge(firstHalf, secondHalf):
45
+        mergeList = []
46
+
47
+        # Mientras ninguna lista este vacia
48
+        while (len(firstHalf) != 0) and (len(secondHalf) != 0):
49
+                
50
+                if firstHalf[0] > secondHalf[0]:
51
+                        n = secondHalf.pop(0)
52
+                        mergeList.append(n)
53
+                else:
54
+                        n = firstHalf.pop(0)
55
+                        mergeList.append(n)
56
+
57
+        # Si la primera mitad no esta vacia, se anade a la lista merge
58
+        while len(firstHalf) != 0:
59
+                mergeList.append(firstHalf[0])
60
+                firstHalf.pop(0)
61
+
62
+        # Si la segunda mitad no esta vacia, se anade a la lista merge
63
+        while len(secondHalf) != 0:
64
+                mergeList.append(secondHalf[0])
65
+                secondHalf.pop(0)
66
+
67
+        return mergeList
68
+                        
69
+        
70
+
28 71
 maxValor=1000 	#define el valor maximo de los elementos de la lista
29 72
 largoLista=1000 #define el largo de las listas a ordenar
30 73
 veces=100 		#define las veces que se va a hacer el ordenamiento 
@@ -40,25 +83,29 @@ for i in range(veces):
40 83
 	quicklista=list(mergelista)
41 84
 	searchlista=list(mergelista)
42 85
 
43
-	t1 = time.clock() 				#seteamos el tiempo al empezar
86
+	t1 = time.time() 				#seteamos el tiempo al empezar
44 87
 	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
45 88
 	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
46 89
 	
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
90
+	# t1 = time.clock()				#seteamos el tiempo al empezar
91
+	# heapSort(heaplista)					#ejecutamos el algoritmo heapSort
92
+	# acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
50 93
 	
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
94
+	# t1 = time.clock()				#seteamos el tiempo al empezar
95
+	# quickSort(quicklista)				#ejecutamos el algoritmo quickSort
96
+	# acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
54 97
 	
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
98
+	# t1 = time.clock()				#seteamos el tiempo al empezar
99
+	# shellSort(searchlista)				#ejecutamos el algoritmo shellSort
100
+	# acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
58 101
 
59 102
 #imprimos los resultados
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"
103
+print("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
104
+print("MergeSort " + str(acumulaMerge/veces) + " segundos")
105
+# print "HeapSort " + str(acumulaHeap/veces) + " segundos"
106
+# print "QuickSort " + str(acumulaQuick/veces) + " segundos"
107
+# print "ShellSort " + str(acumulaShell/veces) + " segundos"
108
+
109
+
110
+
111
+