Bladeren bron

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

José C.S. Curet 1 jaar geleden
bovenliggende
commit
59b18900f3
1 gewijzigde bestanden met toevoegingen van 41 en 12 verwijderingen
  1. 41
    12
      sorting.py

+ 41
- 12
sorting.py Bestand weergeven

@@ -8,13 +8,24 @@ Al final se imprimen los promedios de cada algortimo
8 8
 """
9 9
 from random import randint
10 10
 import time
11
+import math
11 12
 
12 13
 def mergeSort(lista):
13 14
 	#definan el algoritmo de ordenamiento mergesort
14 15
 	return lista
15 16
 
17
+#Esta es la implementación de Heap sort de Geeksforgeeks
18
+#https://www.geeksforgeeks.org/heap-sort/
16 19
 def heapSort(lista):
17 20
 	#definan el algoritmo de ordenamiento heapsort
21
+	n = len(lista)
22
+	#Se hace el maxheap
23
+	for i in range(n // 2 - 1, -1, -1):
24
+		heapify(lista, n, i)
25
+	#Se extraen elementos uno a uno
26
+	for i in range(n - 1, 0, -1):
27
+		(lista[i], lista[0]) = (lista[0], lista[i])
28
+		heapify(lista, i, 0)
18 29
 	return lista
19 30
 
20 31
 """
@@ -48,6 +59,22 @@ def shellSort(lista):
48 59
 	#definan el algoritmo de ordenamiento shellsort
49 60
 	return lista
50 61
 
62
+def heapify(lista, n, i):
63
+	largest = i		#largest = raíz
64
+	l = 2 * i * 1	#left
65
+	r = 2 * i + 2	#right
66
+	#Ver is existe una rama isquierda y si es mayor a la raíz
67
+	if l < n and lista[i] < lista[l]:
68
+		largest = l
69
+	#Ver is existe una rama derecha y si es mayor a la raíz
70
+	if r < n and lista[largest] < lista[r]:
71
+		largest = r
72
+	#Se cambia la raíz si fuese necesario
73
+	if largest != i:
74
+		(lista[i], lista[largest]) = (lista[largest], lista[i])		#swap
75
+		#Se llama heapify en la raíz nueva
76
+		heapify(lista, n, largest)
77
+
51 78
 maxValor=1000 	#define el valor maximo de los elementos de la lista
52 79
 largoLista=1000 #define el largo de las listas a ordenar
53 80
 veces=100 		#define las veces que se va a hacer el ordenamiento 
@@ -63,25 +90,27 @@ for i in range(veces):
63 90
 	quicklista=list(mergelista)
64 91
 	searchlista=list(mergelista)
65 92
 
66
-	# t1 = time.time() 				#seteamos el tiempo al empezar
93
+	# t1 = time.clock() 				#seteamos el tiempo al empezar
67 94
 	# mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
68 95
 	# acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
69 96
 	
70
-	# t1 = time.time()				#seteamos el tiempo al empezar
71
-	# heapSort(heaplista)					#ejecutamos el algoritmo heapSort
72
-	# acumulaHeap+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
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
73 100
 	
74
-	t1 = time.time()				#seteamos el tiempo al empezar
75
-	quickSort(quicklista, 0, len(quicklista) - 1)				#ejecutamos el algoritmo quickSort
76
-	acumulaQuick+=time.time()-t1 	#acumulamos el tiempo de ejecucion
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
77 104
 	
78
-	# t1 = time.time()				#seteamos el tiempo al empezar
105
+	# t1 = time.clock()				#seteamos el tiempo al empezar
79 106
 	# shellSort(searchlista)				#ejecutamos el algoritmo shellSort
80 107
 	# acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
81 108
 
82 109
 #imprimos los resultados
83 110
 print("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
84
-# print("MergeSort " + str(acumulaMerge/veces) + " segundos")
85
-# print("HeapSort " + str(acumulaHeap/veces) + " segundos")
86
-print("QuickSort " + str(acumulaQuick/veces) + " segundos")
87
-# print("ShellSort " + str(acumulaShell/veces) + " segundos")
111
+#print("MergeSort " + str(acumulaMerge/veces) + " segundos")
112
+print("HeapSort " + str(acumulaHeap/veces) + " segundos")
113
+#print("QuickSort " + str(acumulaQuick/veces) + " segundos")
114
+#print("ShellSort " + str(acumulaShell/veces) + " segundos")
115
+
116
+