No Description

sorting.py 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. La variable veces define las veces que se va a hacer el ordenamiento
  8. Al final se imprimen los promedios de cada algortimo
  9. """
  10. from random import randint
  11. import time
  12. from merge import merge
  13. import heapq
  14. # Python program for implementation of MergeSort
  15. # Merges two subarrays of arr[].
  16. # First subarray is arr[l..m]
  17. # Second subarray is arr[m+1..r]
  18. def mergeSort(lista, l, r):
  19. if l < r:
  20. # Same as (l+r)//2, but avoids overflow for
  21. # large l and h
  22. m = l+(r-l)//2
  23. # Sort first and second halves
  24. mergeSort(lista, l, m)
  25. mergeSort(lista, m+1, r)
  26. merge(lista, l, m, r)
  27. ''' Luis Andrés López Mañán
  28. Program written by hand as a draft on 09/19/2022
  29. Credit goes to GeeksForGeeks
  30. Link: https://www.geeksforgeeks.org/python-program-for-heap-sort/
  31. Last access on 09/19/2022
  32. Program wrriten and edited on 10/10/2022
  33. '''
  34. # This function heapifies a subtree rooted at an index
  35. # i. Also, n is the size of a heap and arr is array.
  36. def heapify(lista, n, i):
  37. # largest is root for now
  38. largest = i
  39. # left child of root
  40. l = 2 * i + 1
  41. # right child of root
  42. r = 2 * i + 2
  43. # Checks if root has a left child and is greater than root
  44. if l < n and lista[i] < lista[l] :
  45. largest = l
  46. # Checks if root has a right child and is greater than root
  47. if r < n and lista[largest] < lista[r]:
  48. largest = r
  49. # If necessary, this changes root by swapping values
  50. if largest != i:
  51. lista[i], lista[largest] = lista[largest], lista[i]
  52. # This heapifies the root repeatedly
  53. heapify(lista, n, largest)
  54. def heapSort(lista):
  55. n = len(lista)
  56. n2 = (n // 2) - 1
  57. nMinus = n - 1
  58. for i in range(n2, -1, -1):
  59. heapify(lista, n, i)
  60. for i in range(nMinus, -1, -1):
  61. lista[i], lista[0] = lista[0], lista[i]
  62. heapify(lista, i, 0)
  63. return lista
  64. def quickSort(lista):
  65. #definan el algoritmo de ordenamiento quicksort
  66. return lista
  67. def shellSort(lista):
  68. #definan el algoritmo de ordenamiento shellsort
  69. return lista
  70. maxValor=1000 #define el valor maximo de los elementos de la lista
  71. largoLista=1000 #define el largo de las listas a ordenar
  72. veces=100 #define las veces que se va a hacer el ordenamiento
  73. acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
  74. acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
  75. acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
  76. acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
  77. for i in range(veces):
  78. mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
  79. heaplista=list(mergelista)
  80. quicklista=list(mergelista)
  81. searchlista=list(mergelista)
  82. t1 = time.process_time() #tomamos el tiempo inicial
  83. mergeSort(mergelista,0,len(mergelista)-1) #ejecutamos el algoritmo mergeSort
  84. acumulaMerge+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  85. print(mergelista) #desplegamos la lista
  86. t1 = time.process_time() #tomamos el tiempo inicial
  87. heapSort(heaplista) #ejecutamos el algoritmo heapSort
  88. acumulaHeap+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  89. print(heaplista) #desplegamos la lista
  90. t1 = time.process_time() #tomamos el tiempo inicial
  91. quickSort(quicklista) #ejecutamos el algoritmo quickSort
  92. acumulaQuick+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  93. print(quicklista) #desplegamos la lista
  94. t1 = time.process_time() #tomamos el tiempo inicial
  95. shellSort(searchlista) #ejecutamos el algoritmo shellSort
  96. acumulaShell+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  97. print(searchlista) #desplegamos la lista
  98. #imprimos los resultados
  99. print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
  100. print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
  101. print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
  102. print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
  103. print ("ShellSort " + str(acumulaShell/veces) + " segundos")