forked from GerardoLSJ/matplotlibBASIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbothEXP.py
151 lines (115 loc) · 3.93 KB
/
bothEXP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def insertionSort(n_lista):
for index in range(1,len(n_lista)):
actual = n_lista[index]
posicion = index
print("valor a ordenar = {}".format(actual))
while posicion>0 and n_lista[posicion-1]>actual:
n_lista[posicion]=n_lista[posicion-1]
posicion = posicion-1
n_lista[posicion]=actual
print(n_lista)
print()
return n_lista
#Datos de
"""
lista = [21, 10, 0, 11, 9, 24, 20, 14, 1]
print("lista desordenada {}".format(lista))
insertionSort(lista)
print("lista ordenada {}".format(lista))
"""
print("SEGUNDO METODO")
print("SEGUNDO METODO")
print("SEGUNDO METODO")
print("SEGUNDO METODO")
def quicksort(lista):
quicksort_aux(lista,0,len(lista)-1)
def quicksort_aux(lista,inicio, fin):
if inicio < fin:
pivote = particion(lista,inicio,fin)
quicksort_aux(lista, inicio, pivote-1)
quicksort_aux(lista, pivote+1, fin)
def particion(lista, inicio, fin):
#Se asigna como pivote en número de la primera localidad
pivote = lista[inicio]
print("Valor del pivote {}".format(pivote))
#Se crean dos marcadores
izquierda = inicio+1
derecha = fin
print("Índice izquierdo {}".format(izquierda))
print("Índice derecho {}".format(derecha))
bandera = False
while not bandera:
while izquierda <= derecha and lista[izquierda] <= pivote:
izquierda = izquierda + 1
while lista[derecha] >= pivote and derecha >=izquierda:
derecha = derecha -1
if derecha < izquierda:
bandera= True
else:
temp=lista[izquierda]
lista[izquierda]=lista[derecha]
lista[derecha]=temp
print(lista)
temp=lista[inicio]
lista[inicio]=lista[derecha]
lista[derecha]=temp
return derecha
"""
lista = [21, 10, 0, 11, 9, 24, 20, 14, 1]
print("lista desordenada {}".format(lista))
quicksort(lista)
#Importando librerías
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#print("lista a graficar perrrro {}".format(lista))
"""
#Cargando módulos
import random
from time import time
#Tamaños de la lista de números aleatorios a generar
datos = [ii*5 for ii in range(1,5)]
tiempo_is = [] #Lista para guardar el tiempo de ejecución de insert sort
tiempo_qs = [] #Lista para guardar el tiempo de ejecución de quick sort
for ii in datos:
lista_is = random.sample(range(0, 20), ii)
#Se hace una copia de la lista para que se ejecute el algoritmo con los mismo números
lista_qs = lista_is.copy()
t0 = time() #Se guarda el tiempo inicial
insertionSort(lista_is)
tiempo_is.append(round(time()-t0, 6)) #Se le resta al tiempo actual, el tiempo inicial
t0 = time()
quicksort(lista_qs)
tiempo_qs.append(round(time()-t0, 6))
#Imprimiendo tiempos parciales de ejecución
print("Tiempos parciales de ejecución en INSERT SORT {} \n".format(tiempo_is))
print("Tiempos parciales de ejecución en QUICK SORT {}".format(tiempo_qs))
#Imprimiendo tiempos totales de ejecución
#Para calcular el tiempo total se aplica la función sum() a las listas de tiempo
print("Tiempo total de ejecución en insert sort {}".format(sum(tiempo_is)))
print("Tiempo total de ejecución en quick sort {}".format(sum(tiempo_qs)))
#Generando la gráfica
"""
fig, ax = subplots()
ax.plot(datos, tiempo_is, label="insert sort", marker="*",color="r")
ax.plot(datos, tiempo_qs, label="quick sort", marker="o",color="b")
ax.set_xlabel('Datos')
ax.set_ylabel('Tiempo')
ax.grid(True)
ax.legend(loc=2);
plt.title('Tiempo de ejecución (insert vs. quick)')
plt.show()
"""
from matplotlib import pyplot as plt
from matplotlib import style
style.use('ggplot')
x = datos
y = tiempo_is
x2 = datos
y2 = tiempo_qs
# can plot specifically, after just showing the defaults:
plt.plot(x,y,linewidth=5)
plt.plot(x2,y2,linewidth=5)
plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()