Skip to content

Instantly share code, notes, and snippets.

@jjfajardo
Created February 2, 2012 20:04
Show Gist options
  • Save jjfajardo/1725453 to your computer and use it in GitHub Desktop.
Save jjfajardo/1725453 to your computer and use it in GitHub Desktop.
Implementación del algoritmo de ordenación burbuja en Python.
#! /usr/bin/env python
#Códigos correspondientes al trabajo realizado para el ISUM 2012.
# Test de rendimiento de los algoritmos de ordenamiento Quicksort,
# Mezcla y burbuja implementados en C++, Fortran y Python.
# Guanajuato, Guanajuato, México (14-16 de Marzo 2012)
#
# Programa: burbuja.py
# Uso: $python burbuja.py 1000.dat
# El tamaño del array se toma del nombre del archivo (1000.dat)
# Salida:
# $ Tamaño_array Tiempo_de_ejecución_del_algoritmo
from time import clock
from os import system as sys
def burbuja(valores):
nuevo=valores[:]
for i in range(len(nuevo)):
for j in range(len(nuevo)-1-i):
if nuevo[j] > nuevo[j+1]:
nuevo[j],nuevo[j+1]= nuevo[j+1],nuevo[j]
r=sys('ls -F *.dat | sort -nk1 > lista.txt')
sys('rm timer.txt')
lista=file('lista.txt','r')
comp=lista.readlines()
lineas=len(comp)
lista=file('lista.txt','r')
timer=file('timer.txt','w')
print " largo","\t\t","tiempo"
print "=============================="
for i in range(lineas):
text=lista.readline()
name=text[0:len(text)-1]
data_l=[]
data_file=file(name,'r')
for u in data_file:
uno=u.split()
data_l.append(float(uno[0]))
start=clock()
ordenado=burbuja(data_l)
fin=clock()
tiempo=fin-start
largon=len(data_l)
timer.write('%8.2f\t %3.3f\n' % (largon,tiempo))
print '%8.2f\t %3.3f' % (largon,tiempo)
timer.close()
sys('sort -nk1 timer.txt > tiempo.txt')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment