123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- """
- 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
- import utils.qsortUtils as qsortUtils
-
- def mergeSort(lista):
- #definan el algoritmo de ordenamiento mergesort
- # If the size of the list is greater than 1 then it enters here
- if len(lista) > 1:
- # Gets the half of the list
- Midd = len(lista) // 2
-
- # Takes only the elements that are in the left of the list
- Left = lista[Midd:]
- # If the size of left still is not 1 then
- if len(Left) != 1:
- # it will do a recursive call to the function again
- mergeSort(Left)
- # Takes only the elements that are in the right of the list
- Right = lista[:Midd]
- # If the size of right still is not 1 then
- if len(Right) != 1:
- # it will do a recursive call to the function again
- mergeSort(Right)
-
- # Variables define for while and getting space in the list
- i = j = k = 0
-
- # While i and j are less than the comparation it enters here
- while i < len(Left) and j < len(Right):
- # If the compared number in i is less than j it enters here
- if Left[i] < Right[j]:
- # The less variable is stored in the list again in order
- lista[k] = Left[i]
- i += 1 # Increments i
- # If the compared number in i is greater than j it enters here
- else:
- # The less variable is stored in the list again in order
- lista[k] = Right[j]
- j += 1 # Increments j
- k += 1 # Increments k
-
- # If there are elements remaining in Left the they are put here
- while i < len(Left):
- # The variable that was remaining is put here
- lista[k] = Left[i]
- i += 1 # Increments i
- k += 1 # Increments k
-
- # If there are elements remaining in Right the they are put here
- while j < len(Right):
- # The variable that was remaining is put here
- lista[k] = Right[j]
- j += 1 # Increments j
- k += 1 # Increments k
-
- return lista
- # Code was base and taken from GeeksforGeeks
-
- def heapSort(lista):
- #definan el algoritmo de ordenamiento heapsort
- return lista
-
- def quickSort(lista):
- # Se aplica quicksort a la lista desde el primer elemento hasta el último
- qsortUtils.qSort(lista, 0, len(lista) - 1)
-
- return lista
-
- def shellSort(lista):
- #definan el algoritmo de ordenamiento shellsort
- Size = len(lista) # Contains the complete size of the list
- Diff = Size//2 # Contains the number of half of the list
-
- # Does a insertion sort by ordering in base of the Diff.
- while Diff > 0:
- # Begins a loop to sort the elements added to sorted array.
- for i in range(Diff, Size):
- # Saves the element sorted in a temporary variable
- Tmp = lista[i]
- j = i # Shifts the location of the elements
- # Looks for the locations
- while j >= Diff and lista[j - Diff] > Tmp:
- # Gives the new element to the location
- lista[j] = lista[j - Diff]
- # The size of the array is ajusted
- j -= Diff
- # Puts the Tmp variable in is correct location
- lista[j] = Tmp
- # Reduces again the list
- Diff //= 2
-
- return lista
- # Code was taken from GeeksforGeeks
-
- def main():
- 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=list(mergelista)
- quicklista=list(mergelista)
- searchlista=list(mergelista)
-
- t1 = time.perf_counter() #seteamos el tiempo al empezar
- mergeSort(mergelista) #ejecutamos el algoritmo mergeSort
- acumulaMerge+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
-
- t1 = time.perf_counter() #seteamos el tiempo al empezar
- heapSort(heaplista) #ejecutamos el algoritmo heapSort
- acumulaHeap+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
-
- t1 = time.perf_counter() #seteamos el tiempo al empezar
- quickSort(quicklista) #ejecutamos el algoritmo quickSort
- acumulaQuick+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
-
- t1 = time.perf_counter() #seteamos el tiempo al empezar
- shellSort(searchlista) #ejecutamos el algoritmo shellSort
- acumulaShell+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
-
- #imprimos los resultados
- print (f"Promedio de tiempo de ejecucion de {veces} listas de largo {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")
-
- if __name__ == "__main__":
- main()
|