|
@@ -1,33 +1,150 @@
|
|
1
|
+#coding=utf-8
|
|
2
|
+
|
1
|
3
|
"""
|
|
4
|
+Programadores:
|
2
|
5
|
Carlos J Corrada Bravo
|
|
6
|
+Diego Rodríguez
|
|
7
|
+Joel González
|
|
8
|
+Javier Santiago
|
|
9
|
+Luis Jusino
|
|
10
|
+"""
|
|
11
|
+"""
|
3
|
12
|
Este programa calcula el promedio de tiempo de ejecución de cuatro algoritmos de ordenamiento
|
4
|
13
|
La variable maxValor define el valor maximo de los elementos de la lista
|
5
|
14
|
La variable largoLista define el largo de las listas a ordenar
|
6
|
|
-La variable veces define las veces que se va a hacer el ordenamiento
|
|
15
|
+La variable veces define las veces que se va a hacer el ordenamiento
|
7
|
16
|
Al final se imprimen los promedios de cada algortimo
|
8
|
17
|
"""
|
|
18
|
+
|
9
|
19
|
from random import randint
|
10
|
20
|
import time
|
11
|
21
|
|
12
|
|
-def mergeSort(lista):
|
|
22
|
+
|
|
23
|
+def merge(lista, p, q, r):
|
|
24
|
+ left = lista[p:q+1] # copy the left subarray
|
|
25
|
+ right = lista[q+1:r+1] # copy the right subarray
|
|
26
|
+ i = 0 # index for the left subarray
|
|
27
|
+ j = 0 # index for the right subarray
|
|
28
|
+ k = p # index for the sorted list
|
|
29
|
+
|
|
30
|
+ # Keep adding to the sorted list, while both lists have elements
|
|
31
|
+ while i < len(left) and j < len(right):
|
|
32
|
+ if left[i] <= right[j]:
|
|
33
|
+ lista[k] = left[i]
|
|
34
|
+ i += 1
|
|
35
|
+ else:
|
|
36
|
+ lista[k] = right[j]
|
|
37
|
+ j += 1
|
|
38
|
+
|
|
39
|
+ k += 1
|
|
40
|
+
|
|
41
|
+ # If right finished first, then fill up the rest with the left subarray
|
|
42
|
+ while i < len(left):
|
|
43
|
+ lista[k] = left[i]
|
|
44
|
+ i += 1
|
|
45
|
+ k += 1
|
|
46
|
+
|
|
47
|
+ # If left finished first, then fill up the rest with the right subarray
|
|
48
|
+ while j < len(right):
|
|
49
|
+ lista[k] = right[j]
|
|
50
|
+ j += 1
|
|
51
|
+ k += 1
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+def mergeSortAux(lista, p, r):
|
|
55
|
+ # If array has one element or less, return
|
|
56
|
+ if p >= r:
|
|
57
|
+ return
|
|
58
|
+ # Else, split the array in half
|
|
59
|
+ q = int((p+r)/2) # find the middle
|
|
60
|
+ mergeSortAux(lista, p, q) # Sort the left subarray
|
|
61
|
+ mergeSortAux(lista, q+1, r) # Sort the right subarray
|
|
62
|
+ merge(lista, p, q, r) # Combine both subarrays
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+def mergeSort(listaMerge):
|
13
|
66
|
#definan el algoritmo de ordenamiento mergesort
|
14
|
|
- return lista
|
|
67
|
+ mergeSortAux(listaMerge, 0, len(listaMerge)-1)
|
|
68
|
+ return listaMerge
|
|
69
|
+
|
|
70
|
+#===============================
|
|
71
|
+#Modificación a código: Diego
|
|
72
|
+#Añado función heapify
|
|
73
|
+#===============================
|
|
74
|
+def heapify(listaHeap, largoLista, i):
|
|
75
|
+ largest = i
|
|
76
|
+ left = 2 * i + 1
|
|
77
|
+ right = 2 * i + 2
|
|
78
|
+
|
|
79
|
+ if left < largoLista and listaHeap[i] < listaHeap[left]:
|
|
80
|
+ largest = left
|
|
81
|
+
|
|
82
|
+ if right < largoLista and listaHeap[largest] < listaHeap[right]:
|
|
83
|
+ largest = right
|
15
|
84
|
|
16
|
|
-def heapSort(lista):
|
|
85
|
+ if largest != i:
|
|
86
|
+ listaHeap[i], listaHeap[largest] = listaHeap[largest], listaHeap[i]
|
|
87
|
+ heapify(listaHeap, largoLista, largest)
|
|
88
|
+#Fin de función heapify
|
|
89
|
+#===============================
|
|
90
|
+
|
|
91
|
+def heapSort(listaHeap):
|
17
|
92
|
#definan el algoritmo de ordenamiento heapsort
|
|
93
|
+
|
|
94
|
+ for i in range(len(listaHeap) / 2, -1, -1):
|
|
95
|
+ heapify(listaHeap, len(listaHeap), i)
|
|
96
|
+
|
|
97
|
+ for i in range(len(listaHeap) - 1, 0, -1):
|
|
98
|
+ listaHeap[i], listaHeap[0] = listaHeap[0], listaHeap[i]
|
|
99
|
+ heapify(listaHeap, i, 0)
|
|
100
|
+ return listaHeap
|
|
101
|
+
|
18
|
102
|
return lista
|
19
|
103
|
#Se le da credito al programador de la funcion al final del codigo
|
|
104
|
+
|
20
|
105
|
def quickSort(lista):
|
21
|
106
|
#definan el algoritmo de ordenamiento quicksort
|
22
|
107
|
return lista
|
23
|
108
|
|
24
|
109
|
def shellSort(lista):
|
25
|
|
- #definan el algoritmo de ordenamiento shellsort
|
|
110
|
+ # Subarrays are sorted according to intervals
|
|
111
|
+ # After each set of subarrays is sorted, interval value is updated and process repeats
|
|
112
|
+ # Function stops once iteration with interval = 1 has executed
|
|
113
|
+ # print(lista)
|
|
114
|
+ interval = len(lista) // 2
|
|
115
|
+ while interval > 0:
|
|
116
|
+ # Process repeats for each value between 1 -> interval
|
|
117
|
+ for i in range(0, interval):
|
|
118
|
+ # Starting index determines initial portion of the array that is sorted
|
|
119
|
+ sortedIndex = i
|
|
120
|
+ # Process repeats as long as the current value being considered is greater than the value to its left
|
|
121
|
+ # Being greater than the value to its left means that it is not in the correct location
|
|
122
|
+ j = i
|
|
123
|
+ while j + interval < len(lista):
|
|
124
|
+ if lista[j] > lista[j + interval]:
|
|
125
|
+ # Swapping values so that smaller value is to the left
|
|
126
|
+ temp = lista[j]
|
|
127
|
+ lista[j] = lista[j + interval]
|
|
128
|
+ lista[j + interval] = temp
|
|
129
|
+ # print(lista)
|
|
130
|
+ n = j
|
|
131
|
+ # Continue comparing value that was swapped left to other values to the left to make sure it is placed in the correct location
|
|
132
|
+ while n - interval >= 0 and lista[n] < lista[n - interval]:
|
|
133
|
+ # Swapping values so that smaller value is to the left
|
|
134
|
+ temp = lista[n]
|
|
135
|
+ lista[n] = lista[n - interval]
|
|
136
|
+ lista[n - interval] = temp
|
|
137
|
+ n -= interval
|
|
138
|
+ # print(lista)
|
|
139
|
+ # Update index to continue comparison with the next value in the sub array
|
|
140
|
+ j += interval
|
|
141
|
+ interval //= 2
|
|
142
|
+ # print(lista)
|
26
|
143
|
return lista
|
27
|
144
|
|
28
|
145
|
maxValor=1000 #define el valor maximo de los elementos de la lista
|
29
|
146
|
largoLista=1000 #define el largo de las listas a ordenar
|
30
|
|
-veces=100 #define las veces que se va a hacer el ordenamiento
|
|
147
|
+veces=100 #define las veces que se va a hacer el ordenamiento
|
31
|
148
|
|
32
|
149
|
acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
|
33
|
150
|
acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
|
|
@@ -35,22 +152,28 @@ acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
|
35
|
152
|
acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
|
36
|
153
|
|
37
|
154
|
for i in range(veces):
|
38
|
|
- lista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
|
|
155
|
+ #Creamos una lista con valores al azar
|
|
156
|
+ lista = [randint(0,maxValor) for r in range(largoLista)]
|
|
157
|
+
|
|
158
|
+ listaMerge = lista[:]
|
|
159
|
+ listaHeap = lista[:]
|
|
160
|
+ listaQuick = lista[:]
|
|
161
|
+ listaShell = lista[:]
|
39
|
162
|
|
40
|
163
|
t1 = time.clock() #seteamos el tiempo al empezar
|
41
|
|
- mergeSort(lista) #ejecutamos el algoritmo mergeSort
|
|
164
|
+ mergeSort(listaMerge) #ejecutamos el algoritmo mergeSort
|
42
|
165
|
acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
43
|
|
-
|
|
166
|
+
|
44
|
167
|
t1 = time.clock() #seteamos el tiempo al empezar
|
45
|
|
- heapSort(lista) #ejecutamos el algoritmo heapSort
|
|
168
|
+ heapSort(listaHeap) #ejecutamos el algoritmo heapSort
|
46
|
169
|
acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
47
|
|
-
|
|
170
|
+
|
48
|
171
|
t1 = time.clock() #seteamos el tiempo al empezar
|
49
|
|
- quickSort(lista) #ejecutamos el algoritmo quickSort
|
|
172
|
+ quickSort(listaQuick) #ejecutamos el algoritmo quickSort
|
50
|
173
|
acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
51
|
|
-
|
|
174
|
+
|
52
|
175
|
t1 = time.clock() #seteamos el tiempo al empezar
|
53
|
|
- shellSort(lista) #ejecutamos el algoritmo shellSort
|
|
176
|
+ shellSort(listaShell) #ejecutamos el algoritmo shellSort
|
54
|
177
|
acumulaShell+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
55
|
178
|
|
56
|
179
|
#imprimos los resultados
|