Browse Source

Merge branch 'HeapSort' into sortingFinal

Se hizo in merge del branch HeapSort el cual contiene las funciones
MAX_HEAPIFY, BUILD_MAX_HEAP y heapSort a sortingFinal
ryanbarreto1 1 year ago
parent
commit
7c902df6d4
1 changed files with 85 additions and 39 deletions
  1. 85
    39
      sorting.py

+ 85
- 39
sorting.py View File

@@ -9,6 +9,7 @@ Al final se imprimen los promedios de cada algortimo
9 9
 from random import randint
10 10
 import time
11 11
 import utils.qsortUtils as qsortUtils
12
+import math
12 13
 
13 14
 def mergeSort(lista):
14 15
 	#definan el algoritmo de ordenamiento mergesort
@@ -63,8 +64,49 @@ def mergeSort(lista):
63 64
 	return lista
64 65
 	# Code was base and taken from GeeksforGeeks
65 66
 
67
+def MAX_HEAPIFY(lista, index):
68
+	# funcion para mantener la propiedad lista[PARENT(i)] >= lista[i]
69
+	# para todo nodo excepto la raiz en el heap
70
+
71
+	lefti = 2 * index + 1		# index hijo izquierdo
72
+	righti =  2 * index + 2	# index hijo derecho
73
+
74
+	largo = len(lista) - 1
75
+
76
+	# Si el hijo izq es mayor que el padre
77
+	if lefti <= largo and lista[lefti] > lista[index]:
78
+		largest = lefti
79
+		else:
80
+		largest = index
81
+
82
+	 # Si el hijo derecho es mayor que el padre
83
+	if righti <= largo and lista[righti] > lista[largest]:
84
+		largest = righti
85
+
86
+	 # Si el index seleccionado no es el del numero mas grande
87
+	if largest != index:
88
+		# swap de los numeros en los indexes
89
+		lista[index], lista[largest] = lista[largest], lista[index]
90
+		MAX_HEAPIFY(lista, largest)
91
+
92
+def BUILD_MAX_HEAP(lista):
93
+	largo = len(lista) - 1
94
+	index = math.floor(largo/2)
95
+	while index >= 1:
96
+		MAX_HEAPIFY(lista,index)
97
+		index = index - 1
98
+
66 99
 def heapSort(lista):
67 100
 	#definan el algoritmo de ordenamiento heapsort
101
+
102
+	BUILD_MAX_HEAP(lista)
103
+	largo = len(lista)
104
+	while largo >= 2:
105
+		# swap del primer numero y el ultimo
106
+		lista[0], lista[largo-1] = lista[largo-1], lista[0]
107
+		largo = largo - 1
108
+		MAX_HEAPIFY(lista,0)
109
+
68 110
 	return lista
69 111
 
70 112
 def quickSort(lista):
@@ -97,42 +139,46 @@ def shellSort(lista):
97 139
 	return lista
98 140
 	# Code was taken from GeeksforGeeks
99 141
 
100
-maxValor=1000 	#define el valor maximo de los elementos de la lista
101
-largoLista=1000 #define el largo de las listas a ordenar
102
-veces=100 		#define las veces que se va a hacer el ordenamiento
103
-
104
-acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
105
-acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
106
-acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
107
-
108
-
109
-acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
110
-
111
-for i in range(veces):
112
-	mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
113
-	heaplista=list(mergelista)
114
-	quicklista=list(mergelista)
115
-	searchlista=list(mergelista)
116
-
117
-	t1 = time.perf_counter() 				#seteamos el tiempo al empezar
118
-	mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
119
-	acumulaMerge+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
120
-	
121
-	t1 = time.perf_counter()			#seteamos el tiempo al empezar
122
-	heapSort(heaplista)					#ejecutamos el algoritmo heapSort
123
-	acumulaHeap+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
124
-	
125
-	t1 = time.perf_counter()				#seteamos el tiempo al empezar
126
-	quickSort(quicklista)				#ejecutamos el algoritmo quickSort
127
-	acumulaQuick+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
128
-	
129
-	t1 = time.perf_counter()				#seteamos el tiempo al empezar
130
-	shellSort(searchlista)				#ejecutamos el algoritmo shellSort
131
-	acumulaShell+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
132
-
133
-#imprimos los resultados
134
-print (f"Promedio de tiempo de ejecucion de {veces} listas de largo {largoLista}")
135
-print (f"MergeSort {str(acumulaMerge/veces)} segundos")
136
-print (f"HeapSort {str(acumulaHeap/veces)} segundos")
137
-print (f"QuickSort {str(acumulaQuick/veces)} segundos")
138
-print (f"ShellSort {str(acumulaShell/veces) }segundos")
142
+def main():
143
+	maxValor=1000 	#define el valor maximo de los elementos de la lista
144
+	largoLista=1000 #define el largo de las listas a ordenar
145
+	veces=100 		#define las veces que se va a hacer el ordenamiento
146
+
147
+	acumulaMerge=0 	#variable para acumular el tiempo de ejecucion del mergesort
148
+	acumulaHeap=0 	#variable para acumular el tiempo de ejecucion del heapsort
149
+	acumulaQuick=0 	#variable para acumular el tiempo de ejecucion del quicksort
150
+
151
+
152
+	acumulaShell=0 	#variable para acumular el tiempo de ejecucion del shellsort
153
+
154
+	for i in range(veces):
155
+		mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
156
+		heaplista=list(mergelista)
157
+		quicklista=list(mergelista)
158
+		searchlista=list(mergelista)
159
+
160
+		t1 = time.perf_counter() 				#seteamos el tiempo al empezar
161
+		mergeSort(mergelista) 				#ejecutamos el algoritmo mergeSort
162
+		acumulaMerge+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
163
+		
164
+		t1 = time.perf_counter()			#seteamos el tiempo al empezar
165
+		heapSort(heaplista)					#ejecutamos el algoritmo heapSort
166
+		acumulaHeap+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
167
+		
168
+		t1 = time.perf_counter()				#seteamos el tiempo al empezar
169
+		quickSort(quicklista)				#ejecutamos el algoritmo quickSort
170
+		acumulaQuick+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
171
+		
172
+		t1 = time.perf_counter()				#seteamos el tiempo al empezar
173
+		shellSort(searchlista)				#ejecutamos el algoritmo shellSort
174
+		acumulaShell+=time.perf_counter()-t1 	#acumulamos el tiempo de ejecucion
175
+
176
+	#imprimos los resultados
177
+	print (f"Promedio de tiempo de ejecucion de {veces} listas de largo {largoLista}")
178
+	print (f"MergeSort {str(acumulaMerge/veces)} segundos")
179
+	print (f"HeapSort {str(acumulaHeap/veces)} segundos")
180
+	print (f"QuickSort {str(acumulaQuick/veces)} segundos")
181
+	print (f"ShellSort {str(acumulaShell/veces) }segundos")
182
+
183
+if __name__ == "__main__":
184
+	main()