Last active
September 22, 2017 02:53
-
-
Save joaofeitoza13/b4088d441e8e220bbc59c3f9aa98e0d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 quicksort(array): | |
pivote = len(array) // 2 | |
lesser, equal, greater = [], [], [] | |
for i in range(len(array)): | |
if array[i] < array[pivote]: | |
lesser.append(array[i]) | |
elif array[i] > array[pivote]: | |
greater.append(array[i]) | |
else: equal.append(array[i]) | |
if len(lesser) > 1: | |
lesser = quicksort(lesser) | |
if len(greater) > 1: | |
greater = quicksort(greater) | |
return lesser + equal + greater | |
def testaOrdenamento(quant = 5, tam = 10): | |
for i in range(quant): | |
casoTeste = listaAleatoria(tam) | |
saida = sorted(casoTeste[: ]) | |
assert quicksort(casoTeste) == saida, "Erro {} =/= {} ".format(quicksort(casoTeste), saida) | |
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("quicksort({})".format(lista), setup="from __main__ import quicksort", number=1) | |
temposMedios.append(tempoMedio) | |
listaOtimo = lista[:] | |
listaOtimo.sort() | |
tempoOtimo = timeit("quicksort({})".format(listaOtimo), setup="from __main__ import quicksort", number=1) | |
temposOtimos.append(tempoOtimo) | |
listaPior = listaOtimo[:] | |
listaPior.reverse() | |
tempoPior = timeit("quicksort({})".format(listaPior), setup="from __main__ import quicksort", number=1) | |
temposPiores.append(tempoPior) | |
testaOrdenamento() | |
grafico(tamanhos, temposMedios, temposOtimos, temposPiores) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment