forked from matheushm0/LearningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheapSort.py
64 lines (55 loc) · 1.68 KB
/
heapSort.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
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
from timeit import timeit
from random import randint
def heapsort(lista):
for start in range((len(lista)-2)//2, -1, -1):
siftdown(lista, start, len(lista)-1)
for end in range(len(lista)-1, 0, -1):
lista[end], lista[0] = lista[0], lista[end]
siftdown(lista, 0, end - 1)
return lista
def siftdown(lista, start, end):
root = start
while True:
child = root * 2 + 1
if child > end: break
if child + 1 <= end and lista[child] < lista[child + 1]:
child += 1
if lista[root] < lista[child]:
lista[root], lista[child] = lista[child], lista[root]
root = child
else:
break
def reverse_list(tam):
lista = []
i = 0
while i < tam:
lista.append(tam - i)
i = i + 1
return lista
def random_list(tam):
lista = [ ]
for i in range(tam):
n = randint(1,1*tam)
if n not in lista: lista.append(n)
return lista
def desenhaGrafico(x,y,z,xl = "Elementos", yl = "Tempo"):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
ax.plot(x,y, label = "Pior Caso")
plt.plot(x, z, label="Caso Medio")
ax.legend(bbox_to_anchor=(1, 1),bbox_transform=plt.gcf().transFigure)
plt.ylabel(yl)
plt.xlabel(xl)
fig.savefig('heapsort.png')
length = []
time1 = []
time2 = []
for tams in range(3000, 24001, 3000):
length.append(tams)
time1.append(timeit("heapsort({})".format(random_list(tams), tams),setup="from __main__ import heapsort",number=1))
time2.append(timeit("heapsort({})".format(reverse_list(tams), tams),setup="from __main__ import heapsort",number=1))
desenhaGrafico(length, time1, time2)