forked from matheushm0/LearningPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucketSort.py
82 lines (70 loc) · 1.94 KB
/
bucketSort.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
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate as interpolate
from timeit import timeit
from random import randint
import math
def bucketSort(lists):
code = hashing(lists)
buckets = [list() for _ in range(code[1])]
for i in lists:
x = re_hashing(i, code)
buck = buckets[x]
buck.append(i)
for bucket in buckets:
insertionsort(bucket)
ndx = 0
for b in range(len(buckets)):
for v in buckets[b]:
lists[ndx] = v
ndx += 1
return lists
def hashing(lists):
m = lists[0]
for i in range(1, len(lists)):
if (m < lists[i]):
m = lists[i]
result = [m, int(math.sqrt(len(lists)))]
return result
def re_hashing(i, code):
return int(i / code[0] * (code[1] - 1))
def insertionsort(lists):
for i in range(1, len(lists)):
temp = lists[i]
j = i
while j > 0 and temp < lists[j - 1]:
lists[j] = lists[j - 1]
j -= 1
lists[j] = temp
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('bucket.png')
length = []
time1 = []
time2 = []
for tams in range(3000, 24001, 3000):
length.append(tams)
time1.append(timeit("bucketSort({})".format(random_list(tams), tams),setup="from __main__ import bucketSort",number=1))
time2.append(timeit("bucketSort({})".format(reverse_list(tams), tams),setup="from __main__ import bucketSort",number=1))
desenhaGrafico(length, time1, time2)