Browse Source

HeapSort

Made changes to python 3 and implemented Heap Sort
PabloPuig1 1 year ago
parent
commit
6d3c287a56
1 changed files with 37 additions and 17 deletions
  1. 37
    17
      sorting.py

+ 37
- 17
sorting.py View File

@@ -8,6 +8,7 @@ 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
@@ -15,6 +16,12 @@ def mergeSort(lista):
15 16
 
16 17
 def heapSort(lista):
17 18
 	#definan el algoritmo de ordenamiento heapsort
19
+	n = len(lista)
20
+	for i in range(n // 2 - 1, -1, -1):
21
+		heapify(lista, n, i)
22
+	for i in range(n - 1, 0, -1):
23
+		(lista[i], lista[0]) = (lista[0], lista[i])
24
+		heapify(lista, i, 0)
18 25
 	return lista
19 26
 
20 27
 def quickSort(lista):
@@ -25,6 +32,19 @@ def shellSort(lista):
25 32
 	#definan el algoritmo de ordenamiento shellsort
26 33
 	return lista
27 34
 
35
+def heapify(lista, n, i):
36
+	largest = i
37
+	l = 2 * i * 1
38
+	r = 2 * i + 2
39
+
40
+	if l < n and lista[i] < lista[l]:
41
+		largest = l
42
+	if r < n and lista[largest] < lista[r]:
43
+		largest = r
44
+	if largest != i:
45
+		(lista[i], lista[largest]) = (lista[largest], lista[i])
46
+		heapify(lista, n, largest)
47
+
28 48
 maxValor=1000 	#define el valor maximo de los elementos de la lista
29 49
 largoLista=1000 #define el largo de las listas a ordenar
30 50
 veces=100 		#define las veces que se va a hacer el ordenamiento 
@@ -40,27 +60,27 @@ for i in range(veces):
40 60
 	quicklista=list(mergelista)
41 61
 	searchlista=list(mergelista)
42 62
 
43
-	t1 = time.clock() 				#seteamos el tiempo al empezar
44
-	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
45
-	acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
63
+	# t1 = time.clock() 				#seteamos el tiempo al empezar
64
+	# mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
65
+	# acumulaMerge+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
46 66
 	
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
67
+	t1 = time.time()						#seteamos el tiempo al empezar
68
+	heapSort(heaplista)						#ejecutamos el algoritmo heapSort
69
+	acumulaHeap += time.time() - t1			#acumulamos el tiempo de ejecucion
50 70
 	
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
71
+	# t1 = time.clock()				#seteamos el tiempo al empezar
72
+	# quickSort(quicklista)				#ejecutamos el algoritmo quickSort
73
+	# acumulaQuick+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
54 74
 	
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
75
+	# t1 = time.clock()				#seteamos el tiempo al empezar
76
+	# shellSort(searchlista)				#ejecutamos el algoritmo shellSort
77
+	# acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
58 78
 
59 79
 #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"
80
+print("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
81
+#print("MergeSort " + str(acumulaMerge/veces) + " segundos")
82
+print("HeapSort " + str(acumulaHeap/veces) + " segundos")
83
+#print("QuickSort " + str(acumulaQuick/veces) + " segundos")
84
+#print("ShellSort " + str(acumulaShell/veces) + " segundos")
65 85
 
66 86