No Description

sorting.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. import utils.qsortUtils as qsortUtils
  12. def mergeSort(lista):
  13. #definan el algoritmo de ordenamiento mergesort
  14. # If the size of the list is greater than 1 then it enters here
  15. if len(lista) > 1:
  16. # Gets the half of the list
  17. Midd = len(lista) // 2
  18. # Takes only the elements that are in the left of the list
  19. Left = lista[Midd:]
  20. # If the size of left still is not 1 then
  21. if len(Left) != 1:
  22. # it will do a recursive call to the function again
  23. mergeSort(Left)
  24. # Takes only the elements that are in the right of the list
  25. Right = lista[:Midd]
  26. # If the size of right still is not 1 then
  27. if len(Right) != 1:
  28. # it will do a recursive call to the function again
  29. mergeSort(Right)
  30. # Variables define for while and getting space in the list
  31. i = j = k = 0
  32. # While i and j are less than the comparation it enters here
  33. while i < len(Left) and j < len(Right):
  34. # If the compared number in i is less than j it enters here
  35. if Left[i] < Right[j]:
  36. # The less variable is stored in the list again in order
  37. lista[k] = Left[i]
  38. i += 1 # Increments i
  39. # If the compared number in i is greater than j it enters here
  40. else:
  41. # The less variable is stored in the list again in order
  42. lista[k] = Right[j]
  43. j += 1 # Increments j
  44. k += 1 # Increments k
  45. # If there are elements remaining in Left the they are put here
  46. while i < len(Left):
  47. # The variable that was remaining is put here
  48. lista[k] = Left[i]
  49. i += 1 # Increments i
  50. k += 1 # Increments k
  51. # If there are elements remaining in Right the they are put here
  52. while j < len(Right):
  53. # The variable that was remaining is put here
  54. lista[k] = Right[j]
  55. j += 1 # Increments j
  56. k += 1 # Increments k
  57. return lista
  58. # Code was base and taken from GeeksforGeeks
  59. def heapSort(lista):
  60. #definan el algoritmo de ordenamiento heapsort
  61. return lista
  62. def quickSort(lista):
  63. # Se aplica quicksort a la lista desde el primer elemento hasta el último
  64. qsortUtils.qSort(lista, 0, len(lista) - 1)
  65. return lista
  66. def shellSort(lista):
  67. #definan el algoritmo de ordenamiento shellsort
  68. Size = len(lista) # Contains the complete size of the list
  69. Diff = Size//2 # Contains the number of half of the list
  70. # Does a insertion sort by ordering in base of the Diff.
  71. while Diff > 0:
  72. # Begins a loop to sort the elements added to sorted array.
  73. for i in range(Diff, Size):
  74. # Saves the element sorted in a temporary variable
  75. Tmp = lista[i]
  76. j = i # Shifts the location of the elements
  77. # Looks for the locations
  78. while j >= Diff and lista[j - Diff] > Tmp:
  79. # Gives the new element to the location
  80. lista[j] = lista[j - Diff]
  81. # The size of the array is ajusted
  82. j -= Diff
  83. # Puts the Tmp variable in is correct location
  84. lista[j] = Tmp
  85. # Reduces again the list
  86. Diff //= 2
  87. return lista
  88. # Code was taken from GeeksforGeeks
  89. def main():
  90. maxValor=1000 #define el valor maximo de los elementos de la lista
  91. largoLista=1000 #define el largo de las listas a ordenar
  92. veces=100 #define las veces que se va a hacer el ordenamiento
  93. acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
  94. acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
  95. acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
  96. acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
  97. for i in range(veces):
  98. mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
  99. heaplista=list(mergelista)
  100. quicklista=list(mergelista)
  101. searchlista=list(mergelista)
  102. t1 = time.perf_counter() #seteamos el tiempo al empezar
  103. mergeSort(mergelista) #ejecutamos el algoritmo mergeSort
  104. acumulaMerge+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  105. t1 = time.perf_counter() #seteamos el tiempo al empezar
  106. heapSort(heaplista) #ejecutamos el algoritmo heapSort
  107. acumulaHeap+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  108. t1 = time.perf_counter() #seteamos el tiempo al empezar
  109. quickSort(quicklista) #ejecutamos el algoritmo quickSort
  110. acumulaQuick+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  111. t1 = time.perf_counter() #seteamos el tiempo al empezar
  112. shellSort(searchlista) #ejecutamos el algoritmo shellSort
  113. acumulaShell+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  114. #imprimos los resultados
  115. print (f"Promedio de tiempo de ejecucion de {veces} listas de largo {largoLista}")
  116. print (f"MergeSort {str(acumulaMerge/veces)} segundos")
  117. print (f"HeapSort {str(acumulaHeap/veces)} segundos")
  118. print (f"QuickSort {str(acumulaQuick/veces)} segundos")
  119. print (f"ShellSort {str(acumulaShell/veces) }segundos")
  120. if __name__ == "__main__":
  121. main()