|
@@ -9,86 +9,40 @@ Al final se imprimen los promedios de cada algortimo
|
9
|
9
|
from random import randint
|
10
|
10
|
import time
|
11
|
11
|
|
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):
|
|
12
|
+def mergeSort(lista):
|
53
|
13
|
|
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)
|
|
14
|
+ return lista
|
64
|
15
|
|
|
16
|
+def heapSort(lista):
|
|
17
|
+
|
65
|
18
|
return lista
|
66
|
19
|
|
67
|
|
-
|
68
|
|
-def heapify(lista, n, i):
|
69
|
|
- largest = i
|
70
|
|
- l = 2 * i + 1
|
71
|
|
- r = 2 * i + 2
|
72
|
|
- if l < n and lista[i] < lista[l]:
|
73
|
|
- largest = l
|
74
|
|
- if r < n and lista[largest] < lista[r]:
|
75
|
|
- largest = r
|
76
|
|
- if largest != i:
|
77
|
|
- lista[i], lista[largest] = lista[largest], lista[i]
|
78
|
|
- heapify(lista, n, largest)
|
|
20
|
+def quickSort(lista):
|
|
21
|
+
|
|
22
|
+ elements = len(lista)
|
79
|
23
|
|
80
|
|
-def heapSort(lista):
|
81
|
|
- n = len(lista)
|
|
24
|
+
|
|
25
|
+ if elements < 2:
|
|
26
|
+ return lista
|
82
|
27
|
|
83
|
|
- for i in range(n//2, -1, -1):
|
84
|
|
- heapify(lista, n, i)
|
|
28
|
+ current_position = 0
|
85
|
29
|
|
86
|
|
- for i in range(n-1, 0, -1):
|
87
|
|
- lista[i], lista[0] = lista[0], lista[i]
|
88
|
|
- heapify(lista, i, 0)
|
|
30
|
+ for i in range(1, elements):
|
|
31
|
+ if lista[i] <= lista[0]:
|
|
32
|
+ current_position += 1
|
|
33
|
+ temp = lista[i]
|
|
34
|
+ lista[i] = lista[current_position]
|
|
35
|
+ lista[current_position] = temp
|
89
|
36
|
|
90
|
|
-def quickSort(lista):
|
91
|
|
-
|
|
37
|
+ temp = lista[0]
|
|
38
|
+ lista[0] = lista[current_position]
|
|
39
|
+ lista[current_position] = temp
|
|
40
|
+
|
|
41
|
+ left = quickSort(lista[0:current_position])
|
|
42
|
+ right = quickSort(lista[current_position+1:elements])
|
|
43
|
+
|
|
44
|
+ lista = left + [lista[current_position]] + right
|
|
45
|
+
|
92
|
46
|
return lista
|
93
|
47
|
|
94
|
48
|
def shellSort(lista):
|
|
@@ -111,9 +65,7 @@ for i in range(veces):
|
111
|
65
|
searchlista=list(mergelista)
|
112
|
66
|
|
113
|
67
|
t1 = time.clock()
|
114
|
|
- l = 0
|
115
|
|
- r = len(mergelista) - 1
|
116
|
|
- finished = mergeSort(mergelista, l,r)
|
|
68
|
+ mergeSort(mergelista)
|
117
|
69
|
acumulaMerge+=time.clock()-t1
|
118
|
70
|
|
119
|
71
|
t1 = time.clock()
|
|
@@ -129,7 +81,7 @@ for i in range(veces):
|
129
|
81
|
acumulaShell+=time.clock()-t1
|
130
|
82
|
|
131
|
83
|
|
132
|
|
-print "Promedio de tiempo de ejecucion de " + str(veces) +" listas de largo " + str(largoLista)
|
|
84
|
+print "Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista)
|
133
|
85
|
print "MergeSort " + str(acumulaMerge/veces) + " segundos"
|
134
|
86
|
print "HeapSort " + str(acumulaHeap/veces) + " segundos"
|
135
|
87
|
print "QuickSort " + str(acumulaQuick/veces) + " segundos"
|