Created
February 23, 2018 20:48
-
-
Save joaofeitoza13/c532abb4acbadbc55785cbc8e9fb2e23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 matplotlib.pyplot as plt | |
| from random import randint | |
| from timeit import timeit | |
| def random_array(lenght): | |
| a = [] | |
| while(len(a) < lenght): | |
| x = randint(1, 10*lenght) | |
| if x not in a: | |
| a.append(x) | |
| return a | |
| def bubble(a): | |
| for i in range(0, len(a)-1): | |
| for j in range(i+1, len(a)): | |
| if a[i] > a[j]: | |
| a[j], a[i] = a[i], a[j] | |
| def desenhaGrafico(x, y, xl = "Qnt. Elements(und)", yl = "Time(sec)"): | |
| plt.plot(x, y, label = "Bubblesort") | |
| plt.legend(bbox_to_anchor=(1, 1),bbox_transform=plt.gcf().transFigure) | |
| plt.ylabel(yl) | |
| plt.xlabel(xl) | |
| plt.show() | |
| time = [] | |
| lenght = [] | |
| elements = [1000, 3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000] | |
| def main(): | |
| for _tmp in elements: | |
| array = random_array(_tmp) | |
| time.append(timeit("bubble({})".format(array),setup="from __main__ import bubble", number=1)) | |
| lenght.append(_tmp) | |
| desenhaGrafico(lenght, time) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment