소스 검색

HeapSort

Made changes to python 3 and implemented Heap Sort
PabloPuig1 1 년 전
부모
커밋
6d3c287a56
1개의 변경된 파일37개의 추가작업 그리고 17개의 파일을 삭제
  1. 37
    17
      sorting.py

+ 37
- 17
sorting.py 파일 보기

8
 """
8
 """
9
 from random import randint
9
 from random import randint
10
 import time
10
 import time
11
+import math
11
 
12
 
12
 def mergeSort(lista):
13
 def mergeSort(lista):
13
 	#definan el algoritmo de ordenamiento mergesort
14
 	#definan el algoritmo de ordenamiento mergesort
15
 
16
 
16
 def heapSort(lista):
17
 def heapSort(lista):
17
 	#definan el algoritmo de ordenamiento heapsort
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
 	return lista
25
 	return lista
19
 
26
 
20
 def quickSort(lista):
27
 def quickSort(lista):
25
 	#definan el algoritmo de ordenamiento shellsort
32
 	#definan el algoritmo de ordenamiento shellsort
26
 	return lista
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
 maxValor=1000 	#define el valor maximo de los elementos de la lista
48
 maxValor=1000 	#define el valor maximo de los elementos de la lista
29
 largoLista=1000 #define el largo de las listas a ordenar
49
 largoLista=1000 #define el largo de las listas a ordenar
30
 veces=100 		#define las veces que se va a hacer el ordenamiento 
50
 veces=100 		#define las veces que se va a hacer el ordenamiento 
40
 	quicklista=list(mergelista)
60
 	quicklista=list(mergelista)
41
 	searchlista=list(mergelista)
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
 #imprimos los resultados
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