No Description

sorting.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. for i in range((n // 2) - 1, -1, -1):
  57. heapify(lista, n, i)
  58. for i in range(n-1, -1, -1):
  59. lista[i], lista[0] = lista[0], lista[i]
  60. heapify(lista, i, 0)
  61. return lista
  62. def quickSort(lista):
  63. #definan el algoritmo de ordenamiento quicksort
  64. return lista
  65. def shellSort(lista):
  66. #definan el algoritmo de ordenamiento shellsort
  67. return lista
  68. maxValor=1000 #define el valor maximo de los elementos de la lista
  69. largoLista=1000 #define el largo de las listas a ordenar
  70. veces=100 #define las veces que se va a hacer el ordenamiento
  71. acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
  72. acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
  73. acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
  74. acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
  75. for i in range(veces):
  76. mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
  77. heaplista=list(mergelista)
  78. quicklista=list(mergelista)
  79. searchlista=list(mergelista)
  80. t1 = time.process_time() #tomamos el tiempo inicial
  81. mergeSort(mergelista,0,len(mergelista)-1) #ejecutamos el algoritmo mergeSort
  82. acumulaMerge+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  83. print(mergelista) #desplegamos la lista
  84. t1 = time.process_time() #tomamos el tiempo inicial
  85. heapSort(heaplista) #ejecutamos el algoritmo heapSort
  86. acumulaHeap+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  87. print(heaplista) #desplegamos la lista
  88. t1 = time.process_time() #tomamos el tiempo inicial
  89. quickSort(quicklista) #ejecutamos el algoritmo quickSort
  90. acumulaQuick+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  91. print(quicklista) #desplegamos la lista
  92. t1 = time.process_time() #tomamos el tiempo inicial
  93. shellSort(searchlista) #ejecutamos el algoritmo shellSort
  94. acumulaShell+=time.process_time() - t1 #acumulamos el tiempo de ejecucion
  95. print(searchlista) #desplegamos la lista
  96. #imprimos los resultados
  97. print ("Promedio de tiempo de ejecucion de "+ str(veces) +" listas de largo " + str(largoLista))
  98. print ("MergeSort " + str(acumulaMerge/veces) + " segundos")
  99. print ("HeapSort " + str(acumulaHeap/veces) + " segundos")
  100. print ("QuickSort " + str(acumulaQuick/veces) + " segundos")
  101. print ("ShellSort " + str(acumulaShell/veces) + " segundos")