123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- """
- 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
-
- def mergeSort(lista,start,end):
- #definan el algoritmo de ordenamiento mergesort
- return lista
-
- def heapSort(lista):
- #definan el algoritmo de ordenamiento heapsort
- return lista
-
- def quickSort(lista):
- #definan el algoritmo de ordenamiento quicksort
- return lista
-
- '''
- This algorithm was taken from: https://www.programiz.com/dsa/shell-sort
- and was adapted in order to work for this assigment.
- '''
-
- def shellSort(lista):
- #definan el algoritmo de ordenamiento shellsort
-
- # determening the size of the list and calculates the gap value.
- n = len(lista)
- gap = n // 2
-
- # this algorithm will run until gap reaches 1
- while gap > 0:
- for i in range(gap, n):
- temp = lista[i] # storing all items from the list into temp
- j = i
-
- # compares the number in temp with the 0th possition (start of list)
- # if temp is larger than the first element then we swap them
- while j >= gap and lista[j - gap] > temp:
- lista[j] = lista[j - gap]
- j -= gap
-
- lista[j] = temp
- gap = gap // 2 # decreases the gap to continue the loop
-
- return lista
-
- 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.process_time() #tomamos el tiempo inicial
- mergeSort(mergelista,0,len(mergelista)-1) #ejecutamos el algoritmo mergeSort
- acumulaMerge+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
- #print(mergelista) #desplegamos la lista
-
- t1 = time.process_time() #tomamos el tiempo inicial
- heapSort(heaplista) #ejecutamos el algoritmo heapSort
- acumulaHeap+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
- #print(heaplista) #desplegamos la lista
-
- t1 = time.process_time() #tomamos el tiempo inicial
- quickSort(quicklista) #ejecutamos el algoritmo quickSort
- acumulaQuick+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
- #print(quicklista) #desplegamos la lista
-
- t1 = time.process_time() #tomamos el tiempo inicial
- shellSort(searchlista) #ejecutamos el algoritmo shellSort
- acumulaShell+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
- print(searchlista) #desplegamos la lista
-
- #imprimos los resultados
- print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
- print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
- print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
- print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
- print ("ShellSort " + str(acumulaShell/veces) + " segundos")
|