|
@@ -9,8 +9,59 @@ Al final se imprimen los promedios de cada algortimo
|
9
|
9
|
from random import randint
|
10
|
10
|
import time
|
11
|
11
|
|
12
|
|
-def mergeSort(lista):
|
|
12
|
+def merge(lista, start, middle, end):
|
|
13
|
+
|
|
14
|
+ sizeOfHalf1 = middle - start + 1
|
|
15
|
+ sizeOfHalf2 = end - middle
|
|
16
|
+ half1 = [0] * (sizeOfHalf1)
|
|
17
|
+ half2 = [0] * (sizeOfHalf2)
|
|
18
|
+
|
|
19
|
+ for i in range(0, sizeOfHalf1):
|
|
20
|
+ half1[i] = lista[start + i]
|
|
21
|
+ for t in range(0,sizeOfHalf2):
|
|
22
|
+ half2[t] = lista[middle + 1 + t]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+ i = 0
|
|
26
|
+ t = 0
|
|
27
|
+ k = start
|
|
28
|
+
|
|
29
|
+ while i < sizeOfHalf1 and t < sizeOfHalf2:
|
|
30
|
+ if half1[i] <= half2[t]:
|
|
31
|
+ lista[k] = half1[i]
|
|
32
|
+ i += 1
|
|
33
|
+ else:
|
|
34
|
+ lista[k] = half2[t]
|
|
35
|
+ t += 1
|
|
36
|
+ k += 1
|
|
37
|
+
|
|
38
|
+ while i < sizeOfHalf1:
|
|
39
|
+ lista[k] = half1[i]
|
|
40
|
+ i += 1
|
|
41
|
+ k += 1
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+ while t < sizeOfHalf2:
|
|
45
|
+ lista[k] = half2[t]
|
|
46
|
+ t += 1
|
|
47
|
+ k += 1
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+def mergeSort(lista,l,r):
|
13
|
53
|
|
|
54
|
+ if l < r:
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ m = l+(r-l)//2
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+ mergeSort(lista, l, m)
|
|
61
|
+ mergeSort(lista, m+1, r)
|
|
62
|
+
|
|
63
|
+ merge(lista, l, m, r)
|
|
64
|
+
|
14
|
65
|
return lista
|
15
|
66
|
|
16
|
67
|
|
|
@@ -60,7 +111,9 @@ for i in range(veces):
|
60
|
111
|
searchlista=list(mergelista)
|
61
|
112
|
|
62
|
113
|
t1 = time.clock()
|
63
|
|
- mergeSort(mergelista)
|
|
114
|
+ l = 0
|
|
115
|
+ r = len(mergelista) - 1
|
|
116
|
+ finished = mergeSort(mergelista, l,r)
|
64
|
117
|
acumulaMerge+=time.clock()-t1
|
65
|
118
|
|
66
|
119
|
t1 = time.clock()
|
|
@@ -76,7 +129,7 @@ for i in range(veces):
|
76
|
129
|
acumulaShell+=time.clock()-t1
|
77
|
130
|
|
78
|
131
|
|
79
|
|
-print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
|
|
132
|
+print "Promedio de tiempo de ejecucion de " + str(veces) +" listas de largo " + str(largoLista)
|
80
|
133
|
print "MergeSort " + str(acumulaMerge/veces) + " segundos"
|
81
|
134
|
print "HeapSort " + str(acumulaHeap/veces) + " segundos"
|
82
|
135
|
print "QuickSort " + str(acumulaQuick/veces) + " segundos"
|