Skip to content

Instantly share code, notes, and snippets.

@ejherran
Last active August 29, 2015 14:16
Show Gist options
  • Save ejherran/a5907b61c75fe6882d9f to your computer and use it in GitHub Desktop.
Save ejherran/a5907b61c75fe6882d9f to your computer and use it in GitHub Desktop.
Caso de prueba para comparar los algoritmos de ordenamiento Radix y Bubble.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Use:
# python testSort.py [size] [space] {view}
# size: Tamaño de la muestra.
# space: Limite máximo de los numero aleatorios a generar.
# view: Bandera (1: True, 0: False) que controla la impreesión paso a paso.
#
import random
import time
import sys
def bubbleSort(l, view):
if view:
print "\n"+str(l)+"\n"
i = 0
for i in range(len(l)):
j = 0
for j in range(len(l)-(i+1)):
if l[j] > l[j+1]:
tmp = l[j+1]
l[j+1] = l[j]
l[j] = tmp
if view:
print "\n"+str(l)+"\n"
return l
def getDigito(v, d, t):
v = str(v)
v = '0'*(t-len(v))+v
return v[-1*d]
def radixSort(l, view):
if view:
print "\n"+str(l)+"\n"
lim = l[0]
for e in l:
if lim < e:
lim = e
lim = len(str(lim))
i = 1
while i <= lim:
bk = {'0': [], '1': [], '2': [], '3': [], '4':[], '5':[], '6': [], '7': [], '8': [], '9':[]}
for e in l:
k = getDigito(e, i, lim)
bk[str(k)].append(e)
l = []
for j in range(10):
l += bk[str(j)]
i += 1
if view:
print "\n"+str(l)+"\n"
return l
def test(size, space, view):
print "\n\tEjecutando..."
l1 = []
for i in range(size):
l1.append(random.randint(1, space))
l2 = l1[:]
if view:
print "\n\n\t == Ordenamiento Burbuja == \n"
ini = time.time()
l1 = bubbleSort(l1, view)
timeBubble = time.time()-ini
if view:
print "\n\n\n\t == Ordenamiento Radix == \n"
ini = time.time()
l2 = radixSort(l2, view)
timeRadix = time.time()-ini
if view:
print "\n\n\n\t == RESULTADOS == \n"
print "\n\tMuestra de "+str(size)+" elementos 1 <= X <= "+str(space)
print "\n\tBurbuja:\t"+str(timeBubble)+" segundos."
print "\tRadix:\t\t"+str(timeRadix)+" segundos.\n"
def main():
if len(sys.argv) == 3:
test(int(sys.argv[1]), int(sys.argv[2]), False)
elif len(sys.argv) == 4 and sys.argv[3] == '1':
test(int(sys.argv[1]), int(sys.argv[2]), True)
else:
print "\nUse:\n\tpython testSort.py [size] [space] {view}\n\tsize:\tTamaño de la muestra.\n\tspace:\tLimite máximo de los numero aleatorios a generar.\n\tview:\tBandera (1: True, 0: False) que controla la impreesión paso a paso.\n"
return 0
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment