Skip to content

Instantly share code, notes, and snippets.

@joaofeitoza13
Last active September 29, 2017 02:55
Show Gist options
  • Save joaofeitoza13/cd72656fe055b4e326ddc1328eab8a32 to your computer and use it in GitHub Desktop.
Save joaofeitoza13/cd72656fe055b4e326ddc1328eab8a32 to your computer and use it in GitHub Desktop.
import random
from timeit import timeit
import matplotlib.pyplot as plt
idade = 24
temposMedios = []
temposOtimos = []
temposPiores = []
tamanhos = [100, 300, 600, 900, 1200, 1500, 1800, 2100]
for i in range(len(tamanhos)):
tamanhos[i] = tamanhos[i] * idade
def listaAleatoria(tam):
lista = []
while len(lista) < tam:
n = random.randint(1, 1*tam)
lista.append(n)
return lista
def listaCrescente(tam):
lista = []
for i in range(tam):
lista.append(i)
return lista
def listaDecrescente(tam):
lista = []
for i in range(tam):
lista.append(i)
lista.reverse
return lista
def shellSort(array):
gap = len(array) // 2
while gap > 0:
for i in range(gap):
for j in range(i+gap, len(array), gap):
_tmp = array[j]
pos = j
while pos>=gap and array[pos] > array[pos-gap]:
array[pos] = array[pos-gap]
pos = pos-gap
array[pos] = _tmp
gap = gap // 2
return array
def grafico(x,y1,y2,y3,xl = "Numeros(und)", yl = "Tempo(seg)"):
plt.plot(x, y1, label = "Medio Caso")
plt.plot(x, y2, label = "Melhor Caso")
plt.plot(x, y3, label = "Pior Caso")
legend = plt.legend(loc='upper center', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.xlabel(xl)
plt.ylabel(yl)
plt.show()
for i in tamanhos:
lista = listaAleatoria(i)
tempoMedio = timeit("shellSort({})".format(lista), setup="from __main__ import shellSort", number=1)
temposMedios.append(tempoMedio)
listaOtimo = lista[:]
listaOtimo.sort()
tempoOtimo = timeit("shellSort({})".format(listaOtimo), setup="from __main__ import shellSort", number=1)
temposOtimos.append(tempoOtimo)
listaPior = listaOtimo[:]
listaPior.reverse()
tempoPior = timeit("shellSort({})".format(listaPior), setup="from __main__ import shellSort", number=1)
temposPiores.append(tempoPior)
grafico(tamanhos, temposMedios, temposOtimos, temposPiores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment