Created
March 9, 2018 19:56
-
-
Save joaofeitoza13/4a368654ce3da805b858e3a4bfa16790 to your computer and use it in GitHub Desktop.
Radix sort implementd in python, ploted using matplotlib, smoothed using snipy.interpolate.
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
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 | |
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 radixSort(array): | |
RADIX = 10 | |
maxLength = False | |
tmp = -1 | |
placement = 1 | |
while not maxLength: | |
maxLength = True | |
buckets = [list() for _ in range( RADIX )] | |
for i in array: | |
tmp = i // placement | |
buckets[tmp % RADIX].append( i ) | |
if maxLength and tmp > 0: | |
maxLength = False | |
a = 0 | |
for b in range( RADIX ): | |
buck = buckets[b] | |
for i in buck: | |
array[a] = i | |
a += 1 | |
placement *= RADIX | |
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('Radix 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("radixSort({})".format(arrayRandom), setup = "from __main__ import radixSort", number = 1)) | |
timeReverse.append(timeit("radixSort({})".format(arrayReverse), setup = "from __main__ import radixSort", 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