No Description

sorting.py 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. return qsortUtils.qSort(lista, 0, len(lista) - 1)
  65. def shellSort(lista):
  66. #definan el algoritmo de ordenamiento shellsort
  67. Size = len(lista) # Contains the complete size of the list
  68. Diff = Size/2 # Contains the number of half of the list
  69. # Does a insertion sort by ordering in base of the Diff.
  70. while Diff > 0:
  71. # Begins a loop to sort the elements added to sorted array.
  72. for i in range(Diff, Size):
  73. # Saves the element sorted in a temporary variable
  74. Tmp = lista[i]
  75. j = i # Shifts the location of the elements
  76. # Looks for the locations
  77. while j >= Diff and lista[j - Diff] > Tmp:
  78. # Gives the new element to the location
  79. lista[j] = lista[j - Diff]
  80. # The size of the array is ajusted
  81. j -= Diff
  82. # Puts the Tmp variable in is correct location
  83. lista[j] = Tmp
  84. # Reduces again the list
  85. Diff /= 2
  86. return lista
  87. # Code was taken from GeeksforGeeks
  88. maxValor=1000 #define el valor maximo de los elementos de la lista
  89. largoLista=1000 #define el largo de las listas a ordenar
  90. veces=100 #define las veces que se va a hacer el ordenamiento
  91. acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
  92. acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
  93. acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
  94. acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
  95. for i in range(veces):
  96. mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
  97. heaplista=list(mergelista)
  98. quicklista=list(mergelista)
  99. searchlista=list(mergelista)
  100. t1 = time.perf_counter() #seteamos el tiempo al empezar
  101. mergeSort(mergelista) #ejecutamos el algoritmo mergeSort
  102. acumulaMerge+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  103. t1 = time.perf_counter() #seteamos el tiempo al empezar
  104. heapSort(heaplista) #ejecutamos el algoritmo heapSort
  105. acumulaHeap+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  106. t1 = time.perf_counter() #seteamos el tiempo al empezar
  107. quickSort(quicklista) #ejecutamos el algoritmo quickSort
  108. acumulaQuick+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  109. t1 = time.perf_counter() #seteamos el tiempo al empezar
  110. shellSort(searchlista) #ejecutamos el algoritmo shellSort
  111. acumulaShell+=time.perf_counter()-t1 #acumulamos el tiempo de ejecucion
  112. #imprimos los resultados
  113. print (f"Promedio de tiempo de ejecucion de {veces} listas de largo {largoLista}")
  114. print (f"MergeSort {str(acumulaMerge/veces)} segundos")
  115. print (f"HeapSort {str(acumulaHeap/veces)} segundos")
  116. print (f"QuickSort {str(acumulaQuick/veces)} segundos")
  117. print (f"ShellSort {str(acumulaShell/veces) }segundos")