Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. import sys
  2. import random
  3. import time
  4.  
  5.  
  6. def quicksort(lista):
  7.     if len(lista) < 2:
  8.         return lista
  9.     pivote = lista[0]
  10.     menores = []
  11.     mayores = []
  12.     for i in range(1, len(lista)):
  13.         elemento = lista[i]
  14.         if elemento <= pivote:
  15.             menores.append(elemento)
  16.         else:
  17.             mayores.append(elemento)
  18.  
  19.     return quicksort(menores) + [pivote] + quicksort(mayores)
  20.  
  21.  
  22. def mergesort(lista):
  23.     if len(lista) < 2:
  24.         return lista
  25.     izq = lista[:len(lista)//2]
  26.     der = lista[len(lista)//2:]
  27.     ordenado_izq = mergesort(izq)
  28.     ordenado_der = mergesort(der)
  29.     return merge(ordenado_izq, ordenado_der)
  30.  
  31.  
  32. def merge(a, b):
  33.     resul = []
  34.     cant1 = 0
  35.     cant2 = 0
  36.     while cant1 < len(a) and cant2 < len(b):
  37.         if a[cant1] < b[cant2]:
  38.             resul.append(a[cant1])
  39.             cant1 += 1
  40.         else:
  41.             resul.append(b[cant2])
  42.             cant2 += 1
  43.     resul += a[cant1:]
  44.     resul += b[cant2:]
  45.     return resul
  46.  
  47.  
  48. def swap(lista, pos1, pos2):
  49.     lista[pos1], lista[pos2] = lista[pos2], lista[pos1]
  50.  
  51.  
  52. def posicion_maximo(lista, largo):
  53.     max_pos = 0
  54.     for i in range(largo):
  55.         if lista[i] > lista[max_pos]:
  56.             max_pos = i
  57.     return max_pos
  58.  
  59.  
  60. def seleccion(lista):
  61.     for i in range(len(lista)):
  62.         pos_max = posicion_maximo(lista, len(lista) - i)
  63.         swap(lista, pos_max, len(lista) - i - 1)
  64.  
  65. def insercion(lista):
  66.     for i in range(1, len(lista)):
  67.         x = lista[i]
  68.         j = i
  69.         while j > 0 and lista[j-1] > x:
  70.             lista[j] = lista[j-1]
  71.             j = j - 1
  72.         lista[j] = x
  73.  
  74. def bench(lista_desordenada, f, nombre):
  75.     print("{: <10s}".format(nombre), end="")
  76.     sys.stdout.flush()
  77.     start = time.time()
  78.     f(lista_desordenada[:])
  79.     end = time.time()
  80.     print("{:8.4f} segundos".format(end - start))
  81.  
  82. def main(cantidad):
  83.     lista_ordenada = list(range(cantidad))
  84.     lista_desordenada = lista_ordenada[:]
  85.     random.shuffle(lista_desordenada)
  86.  
  87.     bench(lista_desordenada, quicksort, "Quicksort")
  88.     bench(lista_desordenada, mergesort, "Mergesort")
  89.     bench(lista_ordenada, quicksort, "Quicksort Ordenado")
  90.     bench(lista_desordenada, seleccion, "Seleccion")
  91.     bench(lista_desordenada, insercion, "Insercion")
  92.  
  93. if __name__ == "__main__":
  94.     if len(sys.argv) != 2 or not sys.argv[1].isdigit():
  95.         print("Uso: python ordenamientos.py [cantidad de elementos a ordenar]")
  96.     else:
  97.         main(int(sys.argv[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement