No Description

sorting.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. Carlos J Corrada Bravo
  3. Este programa calcula el promedio de tiempo de ejecucion de cuatro algoritmos de ordenamiento
  4. La variable maxValor define el valor maximo de los elementos de la lista
  5. La variable largoLista define el largo de las listas a ordenar
  6. La variable veces define las veces que se va a hacer el ordenamiento
  7. Al final se imprimen los promedios de cada algortimo
  8. """
  9. from random import randint
  10. import time
  11. def mergeSort(lista,start,end):
  12. #definan el algoritmo de ordenamiento mergesort
  13. return lista
  14. def heapSort(lista):
  15. #definan el algoritmo de ordenamiento heapsort
  16. return lista
  17. def quickSort(lista):
  18. #definan el algoritmo de ordenamiento quicksort
  19. return lista
  20. '''
  21. This algorithm was taken from: https://www.programiz.com/dsa/shell-sort
  22. and was adapted in order to work for this assigment.
  23. '''
  24. def shellSort(lista):
  25. #definan el algoritmo de ordenamiento shellsort
  26. # determening the size of the list and calculates the gap value.
  27. n = len(lista)
  28. gap = n // 2
  29. # this algorithm will run until gap reaches 1
  30. while gap > 0:
  31. for i in range(gap, n):
  32. temp = lista[i] # storing all items from the list into temp
  33. j = i
  34. # compares the number in temp with the 0th possition (start of list)
  35. # if temp is larger than the first element then we swap them
  36. while j >= gap and lista[j - gap] > temp:
  37. lista[j] = lista[j - gap]
  38. j -= gap
  39. lista[j] = temp
  40. gap = gap // 2 # decreases the gap to continue the loop
  41. return lista
  42. maxValor=1000 #define el valor maximo de los elementos de la lista
  43. largoLista=1000 #define el largo de las listas a ordenar
  44. veces=100 #define las veces que se va a hacer el ordenamiento
  45. acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
  46. acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
  47. acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
  48. acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
  49. for i in range(veces):
  50. mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
  51. heaplista=list(mergelista)
  52. quicklista=list(mergelista)
  53. searchlista=list(mergelista)
  54. t1 = time.process_time() #tomamos el tiempo inicial
  55. mergeSort(mergelista,0,len(mergelista)-1) #ejecutamos el algoritmo mergeSort
  56. acumulaMerge+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  57. #print(mergelista) #desplegamos la lista
  58. t1 = time.process_time() #tomamos el tiempo inicial
  59. heapSort(heaplista) #ejecutamos el algoritmo heapSort
  60. acumulaHeap+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  61. #print(heaplista) #desplegamos la lista
  62. t1 = time.process_time() #tomamos el tiempo inicial
  63. quickSort(quicklista) #ejecutamos el algoritmo quickSort
  64. acumulaQuick+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  65. #print(quicklista) #desplegamos la lista
  66. t1 = time.process_time() #tomamos el tiempo inicial
  67. shellSort(searchlista) #ejecutamos el algoritmo shellSort
  68. acumulaShell+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  69. print(searchlista) #desplegamos la lista
  70. #imprimos los resultados
  71. print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
  72. print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
  73. print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
  74. print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
  75. print ("ShellSort " + str(acumulaShell/veces) + " segundos")