1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- """
- Carlos J Corrada Bravo
- Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
- La variable maxValor define el valor maximo de los elementos de la lista
- La variable largoLista define el largo de las listas a ordenar
- La variable veces define las veces que se va a hacer el ordenamiento
- Al final se imprimen los promedios de cada algortimo
- """
- from random import randint
- import time
- from copy import deepcopy
- import sys
-
- # This function was created to prevent program from stopping before recursion finished
- # Changes python's recursion limit
- class recursion_depth:
- def __init__(self, limit):
- self.limit = limit
- self.default_limit = sys.getrecursionlimit()
-
- def __enter__(self):
- sys.setrecursionlimit(self.limit)
-
- def __exit__(self, type, value, traceback):
- sys.setrecursionlimit(self.default_limit)
-
- # Mergesort algorithm
- def mergeSort(lista): # Ángel G. Romero Rosario on 10082022
-
- def merge(l1, l2):
- if len(l1) == 0:
- return l2
- elif len(l2) == 0:
- return l1
- elif l1[0] < l2[0]:
- return l1[0:1] + merge(l1[1:],l2) # If l1[0] < l2[0] save l1[0] first to the list and call the function again
- else:
- return l2[0:1] + merge(l1, l2[1:]) # If l2[0] < l1[0] save l2[0] first to the list and call the function again
-
- if len(lista) <= 1: # If there are no more items, return lista
- return lista
-
- else:
- mid = len(lista) // 2 # Find the middle in lista and call function to merge lista
- return merge(mergeSort(lista[:mid]), mergeSort(lista[mid:]))
-
-
- def heapSort(lista):
- #definan el algoritmo de ordenamiento heapsort
- return lista
-
- def quickSort(lista):
- #definan el algoritmo de ordenamiento quicksort
- return lista
-
- def shellSort(lista):
- #definan el algoritmo de ordenamiento shellsort
- return lista
-
- # timeCode function/thunk -> (duration, return value)
- # measures the time it takes for a function/thunk to return
- def timeCode(fn):
- t1 = time.perf_counter()
- res = fn()
- duration = time.perf_counter() - t1
- return (duration, res)
-
- maxValor = 1000 #define el valor maximo de los elementos de la lista
- largoLista = 1000 #define el largo de las listas a ordenar
- veces = 100 #define las veces que se va a hacer el ordenamiento
-
- acumulaMerge = 0 #variable para acumular el tiempo de ejecucion del mergesort
- acumulaHeap = 0 #variable para acumular el tiempo de ejecucion del heapsort
- acumulaQuick = 0 #variable para acumular el tiempo de ejecucion del quicksort
- acumulaShell = 0 #variable para acumular el tiempo de ejecucion del shellsort
-
- for i in range(veces):
- mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
- heaplista = deepcopy(mergelista)
- quicklista = deepcopy(mergelista)
- searchlista = deepcopy(mergelista)
-
- with recursion_depth(1500): # This function excedes python's recursion limit
- acumulaMerge += timeCode(lambda: mergeSort(mergelista))[0]
-
- acumulaHeap += timeCode(lambda: heapSort(heaplista))[0]
- acumulaQuick += timeCode(lambda: quickSort(quicklista))[0]
- acumulaShell += timeCode(lambda: shellSort(searchlista))[0]
-
- #imprimos los resultados
- print(f"Promedio de tiempo de ejecucion de {str(veces)} listas de largo {str(largoLista)}")
- print(f"MergeSort {str(acumulaMerge / veces)} segundos")
- print(f"HeapSort {str(acumulaHeap / veces)} segundos")
- print(f"QuickSort {str(acumulaQuick / veces)} segundos")
- print(f"ShellSort {str(acumulaShell / veces)} segundos")
|