Bladeren bron

algorithm

andrea.nieves7 1 jaar geleden
bovenliggende
commit
58679f10cc
1 gewijzigde bestanden met toevoegingen van 46 en 8 verwijderingen
  1. 46
    8
      sorting.py

+ 46
- 8
sorting.py Bestand weergeven

@@ -11,11 +11,49 @@ import time
11 11
 
12 12
 def mergeSort(lista):
13 13
 	#definan el algoritmo de ordenamiento mergesort
14
-	return lista
14
+    return lista
15
+    
15 16
 
16 17
 def heapSort(lista):
17
-	#definan el algoritmo de ordenamiento heapsort
18
-	return lista
18
+	    
19
+    
20
+    #Andrea V. Nieves
21
+    
22
+    
23
+    #function def
24
+    def heapify(lista, n, i):
25
+       biggest = i
26
+       left = 2*i + 1
27
+       right = 2*i + 2
28
+       
29
+       
30
+       if left < n and lista[left] > lista[i]:
31
+           biggest = left
32
+       else:
33
+            biggest = i
34
+            
35
+       if right< n and lista[right] > lista[biggest]:
36
+           biggest = right
37
+           
38
+       if biggest != i:
39
+           lista[i], lista[biggest] = lista[biggest], lista[i]
40
+           heapify(lista,n,biggest)
41
+    
42
+    
43
+   
44
+    
45
+       #actual call
46
+    n = len(lista)
47
+    
48
+    for i in range(n // 2 - 1, -1, -1):
49
+        heapify(lista, n, i)
50
+        
51
+    for i in range(n - 1, 0, -1):
52
+        lista[i], lista[0] = lista[0], lista[i]
53
+        heapify(lista, i, 0)
54
+        
55
+    
56
+    return lista
19 57
 
20 58
 def quickSort(lista):
21 59
 	#definan el algoritmo de ordenamiento quicksort
@@ -57,9 +95,9 @@ for i in range(veces):
57 95
 	acumulaShell+=time.clock()-t1 	#acumulamos el tiempo de ejecucion
58 96
 
59 97
 #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"
98
+#print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
99
+#print "MergeSort " + str(acumulaMerge/veces) + " segundos"
100
+#print "HeapSort " + str(acumulaHeap/veces) + " segundos"
101
+#print "QuickSort " + str(acumulaQuick/veces) + " segundos"
102
+#print "ShellSort " + str(acumulaShell/veces) + " segundos"
65 103