|
|
|
|
3
|
Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
|
3
|
Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
|
4
|
La variable maxValor define el valor maximo de los elementos de la lista
|
4
|
La variable maxValor define el valor maximo de los elementos de la lista
|
5
|
La variable largoLista define el largo de las listas a ordenar
|
5
|
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
|
|
|
|
|
6
|
+La variable veces define las veces que se va a hacer el ordenamiento
|
7
|
Al final se imprimen los promedios de cada algortimo
|
7
|
Al final se imprimen los promedios de cada algortimo
|
8
|
"""
|
8
|
"""
|
9
|
from random import randint
|
9
|
from random import randint
|
|
|
|
|
11
|
|
11
|
|
12
|
def mergeSort(lista):
|
12
|
def mergeSort(lista):
|
13
|
#definan el algoritmo de ordenamiento mergesort
|
13
|
#definan el algoritmo de ordenamiento mergesort
|
|
|
14
|
+ # If the size of the list is greater than 1 then it enters here
|
|
|
15
|
+ if len(lista) > 1:
|
|
|
16
|
+ # Gets the half of the list
|
|
|
17
|
+ Midd = len(lista) / 2
|
|
|
18
|
+ # Takes only the elements that are in the left of the list
|
|
|
19
|
+ Left = lista[Midd:]
|
|
|
20
|
+ # If the size of left still is not 1 then
|
|
|
21
|
+ if len(Left) != 1:
|
|
|
22
|
+ # it will do a recursive call to the function again
|
|
|
23
|
+ mergeSort(Left)
|
|
|
24
|
+ # Takes only the elements that are in the right of the list
|
|
|
25
|
+ Right = lista[:Midd]
|
|
|
26
|
+ # If the size of right still is not 1 then
|
|
|
27
|
+ if len(Right) != 1:
|
|
|
28
|
+ # it will do a recursive call to the function again
|
|
|
29
|
+ mergeSort(Right)
|
|
|
30
|
+
|
|
|
31
|
+ # Variables define for while and getting space in the list
|
|
|
32
|
+ i = j = k = 0
|
|
|
33
|
+
|
|
|
34
|
+ # While i and j are less than the comparation it enters here
|
|
|
35
|
+ while i < len(Left) and j < len(Right):
|
|
|
36
|
+ # If the compared number in i is less than j it enters here
|
|
|
37
|
+ if Left[i] < Right[j]:
|
|
|
38
|
+ # The less variable is stored in the list again in order
|
|
|
39
|
+ lista[k] = Left[i]
|
|
|
40
|
+ i += 1 # Increments i
|
|
|
41
|
+ # If the compared number in i is greater than j it enters here
|
|
|
42
|
+ else:
|
|
|
43
|
+ # The less variable is stored in the list again in order
|
|
|
44
|
+ lista[k] = Right[j]
|
|
|
45
|
+ j += 1 # Increments j
|
|
|
46
|
+ k += 1 # Increments k
|
|
|
47
|
+
|
|
|
48
|
+ # If there are elements remaining in Left the they are put here
|
|
|
49
|
+ while i < len(Left):
|
|
|
50
|
+ # The variable that was remaining is put here
|
|
|
51
|
+ lista[k] = Left[i]
|
|
|
52
|
+ i += 1 # Increments i
|
|
|
53
|
+ k += 1 # Increments k
|
|
|
54
|
+
|
|
|
55
|
+ # If there are elements remaining in Right the they are put here
|
|
|
56
|
+ while j < len(Right):
|
|
|
57
|
+ # The variable that was remaining is put here
|
|
|
58
|
+ lista[k] = Right[j]
|
|
|
59
|
+ j += 1 # Increments j
|
|
|
60
|
+ k += 1 # Increments k
|
|
|
61
|
+
|
14
|
return lista
|
62
|
return lista
|
|
|
63
|
+ # Code was base and taken from GeeksforGeeks
|
15
|
|
64
|
|
16
|
def heapSort(lista):
|
65
|
def heapSort(lista):
|
17
|
#definan el algoritmo de ordenamiento heapsort
|
66
|
#definan el algoritmo de ordenamiento heapsort
|
|
|
|
|
23
|
|
72
|
|
24
|
def shellSort(lista):
|
73
|
def shellSort(lista):
|
25
|
#definan el algoritmo de ordenamiento shellsort
|
74
|
#definan el algoritmo de ordenamiento shellsort
|
|
|
75
|
+ Size = len(lista) # Contains the complete size of the list
|
|
|
76
|
+ Diff = Size/2 # Contains the number of half of the list
|
|
|
77
|
+
|
|
|
78
|
+ # Does a insertion sort by ordering in base of the Diff.
|
|
|
79
|
+ while Diff > 0:
|
|
|
80
|
+ # Begins a loop to sort the elements added to sorted array.
|
|
|
81
|
+ for i in range(Diff, Size):
|
|
|
82
|
+ # Saves the element sorted in a temporary variable
|
|
|
83
|
+ Tmp = lista[i]
|
|
|
84
|
+ j = i # Shifts the location of the elements
|
|
|
85
|
+ # Looks for the locations
|
|
|
86
|
+ while j >= Diff and lista[j - Diff] > Tmp:
|
|
|
87
|
+ # Gives the new element to the location
|
|
|
88
|
+ lista[j] = lista[j - Diff]
|
|
|
89
|
+ # The size of the array is ajusted
|
|
|
90
|
+ j -= Diff
|
|
|
91
|
+ # Puts the Tmp variable in is correct location
|
|
|
92
|
+ lista[j] = Tmp
|
|
|
93
|
+ # Reduces again the list
|
|
|
94
|
+ Diff /= 2
|
|
|
95
|
+
|
26
|
return lista
|
96
|
return lista
|
|
|
97
|
+ # Code was taken from GeeksforGeeks
|
27
|
|
98
|
|
28
|
maxValor=1000 #define el valor maximo de los elementos de la lista
|
99
|
maxValor=1000 #define el valor maximo de los elementos de la lista
|
29
|
largoLista=1000 #define el largo de las listas a ordenar
|
100
|
largoLista=1000 #define el largo de las listas a ordenar
|
30
|
-veces=100 #define las veces que se va a hacer el ordenamiento
|
|
|
|
|
101
|
+veces=100 #define las veces que se va a hacer el ordenamiento
|
31
|
|
102
|
|
32
|
acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
|
103
|
acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
|
33
|
acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
|
104
|
acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
|
|
|
|
|
43
|
t1 = time.clock() #seteamos el tiempo al empezar
|
114
|
t1 = time.clock() #seteamos el tiempo al empezar
|
44
|
mergeSort(mergelista) #ejecutamos el algoritmo mergeSort
|
115
|
mergeSort(mergelista) #ejecutamos el algoritmo mergeSort
|
45
|
acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
116
|
acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
46
|
-
|
|
|
|
|
117
|
+
|
47
|
t1 = time.clock() #seteamos el tiempo al empezar
|
118
|
t1 = time.clock() #seteamos el tiempo al empezar
|
48
|
heapSort(heaplista) #ejecutamos el algoritmo heapSort
|
119
|
heapSort(heaplista) #ejecutamos el algoritmo heapSort
|
49
|
acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
120
|
acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
50
|
-
|
|
|
|
|
121
|
+
|
51
|
t1 = time.clock() #seteamos el tiempo al empezar
|
122
|
t1 = time.clock() #seteamos el tiempo al empezar
|
52
|
quickSort(quicklista) #ejecutamos el algoritmo quickSort
|
123
|
quickSort(quicklista) #ejecutamos el algoritmo quickSort
|
53
|
acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
124
|
acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
54
|
-
|
|
|
|
|
125
|
+
|
55
|
t1 = time.clock() #seteamos el tiempo al empezar
|
126
|
t1 = time.clock() #seteamos el tiempo al empezar
|
56
|
shellSort(searchlista) #ejecutamos el algoritmo shellSort
|
127
|
shellSort(searchlista) #ejecutamos el algoritmo shellSort
|
57
|
acumulaShell+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
128
|
acumulaShell+=time.clock()-t1 #acumulamos el tiempo de ejecucion
|
|
|
|
|
62
|
print "HeapSort " + str(acumulaHeap/veces) + " segundos"
|
133
|
print "HeapSort " + str(acumulaHeap/veces) + " segundos"
|
63
|
print "QuickSort " + str(acumulaQuick/veces) + " segundos"
|
134
|
print "QuickSort " + str(acumulaQuick/veces) + " segundos"
|
64
|
print "ShellSort " + str(acumulaShell/veces) + " segundos"
|
135
|
print "ShellSort " + str(acumulaShell/veces) + " segundos"
|
65
|
-
|
|
|