Skip to content

Instantly share code, notes, and snippets.

@joaofeitoza13
Last active March 14, 2018 21:12
Show Gist options
  • Save joaofeitoza13/9e36813d5d0b30e4367a7ae2645805d7 to your computer and use it in GitHub Desktop.
Save joaofeitoza13/9e36813d5d0b30e4367a7ae2645805d7 to your computer and use it in GitHub Desktop.
Bucket Sort implemented in python, plot using matplotlib.
from random import randint
from timeit import timeit
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import scipy.interpolate as interpolate
import math
def reverseArray(lenght):
a = []
for i in range(lenght):
a.append(lenght-i)
return a
def randomArray(length):
array = []
tmp = 0
while tmp < length:
num = randint(1, length*10)
if num not in array:
array.append(num)
tmp += 1
return array
def insertionSort(array):
for i in range(1, len(array)):
_tmp = array[i]
j = i - 1
while j >= 0 and _tmp < array[j]:
array[j+1] = array[j]
j -= 1
array[j+1] = _tmp
def hashing(array):
m = array[0]
for i in range(1, len(array)):
if m < array[i]:
m = array[i]
result = [m,int(math.sqrt( len(array)))]
return result
def re_hashing(i, code ):
return int(i/code[0]*(code[1]-1))
def bucketSort(array):
code = hashing(array)
buckets = [list() for _ in range( code[1] )]
for i in array:
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]:
array[ndx] = v
ndx += 1
timeRandom = []
timeReverse = []
lengths = []
elements = [1000, 3000, 6000, 9000, 12000, 15000, 18000, 21000, 24000]
def desenhaGrafico(x, y, z, xl = "Qnt. Elements(und)", yl = "Time(sec)"):
fig = plt.figure(figsize=(10, 8))
xnew = np.linspace(min(x), max(x), 10 * ( max(x) - min(x)))
t, c, k = interpolate.splrep(x, y, s=0, k=2)
smooth1 = interpolate.BSpline(t, c, k, extrapolate=False)
m, n, o = interpolate.splrep(x, z, s=0, k=2)
smooth2 = interpolate.BSpline(m, n, o, extrapolate=False)
plt.subplot(211)
plt.title('Bucket Sort Analysis', fontsize=20)
plt.plot(x, y, label = "Random")
plt.plot(x, z, label = "Reverse")
legend = plt.legend(loc='upper left', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.subplot(212)
plt.plot(xnew, smooth1(xnew), label = "Random - Smooth")
plt.plot(xnew, smooth2(xnew), label = "Reverse - Smooth")
legend = plt.legend(loc='upper left', shadow=True)
frame = legend.get_frame()
frame.set_facecolor('0.90')
plt.xlabel(xl)
plt.ylabel(yl)
fig.savefig('radix.png')
def main():
for _tmp in elements:
lengths.append(_tmp)
arrayRandom = randomArray(_tmp)
arrayReverse = reverseArray(_tmp)
timeRandom.append(timeit("bucketSort({})".format(arrayRandom), setup = "from __main__ import bucketSort", number = 1))
timeReverse.append(timeit("bucketSort({})".format(arrayReverse), setup = "from __main__ import bucketSort", number = 1))
desenhaGrafico(lengths, timeRandom, timeReverse)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment