No Description

sorting.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 merge(lista, start, middle, end):
  12. # Create Temp arrays with specifically needed sizes foe the given array
  13. sizeOfHalf1 = middle - start + 1
  14. sizeOfHalf2 = end - middle
  15. half1 = [0] * (sizeOfHalf1)
  16. half2 = [0] * (sizeOfHalf2)
  17. # Copy data from the received array to the temporary working arrays
  18. for i in range(0, sizeOfHalf1):
  19. half1[i] = lista[start + i]
  20. for t in range(0,sizeOfHalf2):
  21. half2[t] = lista[middle + 1 + t]
  22. # Merge the temporary arrays back into the recieved list
  23. i = 0 # initial index of firs subarray
  24. t = 0 # initial index of second subarray
  25. k = start # initial index of merged subarray
  26. while i < sizeOfHalf1 and t < sizeOfHalf2:
  27. if half1[i] <= half2[t]:
  28. lista[k] = half1[i]
  29. i += 1
  30. else:
  31. lista[k] = half2[t]
  32. t += 1
  33. k += 1
  34. # Copy the remaining elements on half1 if there are any
  35. while i < sizeOfHalf1:
  36. lista[k] = half1[i]
  37. i += 1
  38. k += 1
  39. # Copy the remaining elements on half2 if there are any
  40. while t < sizeOfHalf2:
  41. lista[k] = half2[t]
  42. t += 1
  43. k += 1
  44. # start is for left index and end is the right index of the subarray
  45. # of lista to be stored
  46. def mergeSort(lista,l,r):
  47. #definan el algoritmo de ordenamiento mergesort
  48. if l < r:
  49. # Same as (l+r)//2, but avoids overflow for
  50. # large l and h
  51. m = l+(r-l)//2
  52. # Sort each half
  53. mergeSort(lista, l, m)
  54. mergeSort(lista, m+1, r)
  55. # Merge the sorted halves
  56. merge(lista, l, m, r)
  57. return lista
  58. def heapSort(lista):
  59. #definan el algoritmo de ordenamiento heapsort
  60. return lista
  61. def quickSort(lista):
  62. #definan el algoritmo de ordenamiento quicksort
  63. return lista
  64. def shellSort(lista):
  65. #definan el algoritmo de ordenamiento shellsort
  66. return lista
  67. maxValor=1000 #define el valor maximo de los elementos de la lista
  68. largoLista=1000 #define el largo de las listas a ordenar
  69. veces=100 #define las veces que se va a hacer el ordenamiento
  70. acumulaMerge=0 #variable para acumular el tiempo de ejecucion del mergesort
  71. acumulaHeap=0 #variable para acumular el tiempo de ejecucion del heapsort
  72. acumulaQuick=0 #variable para acumular el tiempo de ejecucion del quicksort
  73. acumulaShell=0 #variable para acumular el tiempo de ejecucion del shellsort
  74. for i in range(veces):
  75. mergelista = [randint(0,maxValor) for r in range(largoLista)] #creamos una lista con valores al azar
  76. heaplista=list(mergelista)
  77. quicklista=list(mergelista)
  78. searchlista=list(mergelista)
  79. t1 = time.clock() #seteamos el tiempo al empezar
  80. l = 0
  81. r = len(mergelista) - 1
  82. finished = mergeSort(mergelista, l,r) #ejecutamos el algoritmo mergeSort
  83. acumulaMerge+=time.clock()-t1 #acumulamos el tiempo de ejecucion
  84. t1 = time.clock() #seteamos el tiempo al empezar
  85. heapSort(heaplista) #ejecutamos el algoritmo heapSort
  86. acumulaHeap+=time.clock()-t1 #acumulamos el tiempo de ejecucion
  87. t1 = time.clock() #seteamos el tiempo al empezar
  88. quickSort(quicklista) #ejecutamos el algoritmo quickSort
  89. acumulaQuick+=time.clock()-t1 #acumulamos el tiempo de ejecucion
  90. t1 = time.clock() #seteamos el tiempo al empezar
  91. shellSort(searchlista) #ejecutamos el algoritmo shellSort
  92. acumulaShell+=time.clock()-t1 #acumulamos el tiempo de ejecucion
  93. #imprimos los resultados
  94. print "Promedio de tiempo de ejecucion de " + str(veces) +" listas de largo " + str(largoLista)
  95. print "MergeSort " + str(acumulaMerge/veces) + " segundos"
  96. print "HeapSort " + str(acumulaHeap/veces) + " segundos"
  97. print "QuickSort " + str(acumulaQuick/veces) + " segundos"
  98. print "ShellSort " + str(acumulaShell/veces) + " segundos"