Guest User

Untitled

a guest
Apr 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import matplotlib as mpl
  2. mpl.use('Agg')
  3. import matplotlib.pyplot as plt
  4. from random import randint
  5.  
  6. def geraLista(tam):
  7. lista = []
  8. for i in range(tam):
  9. n = randint(1,1*tam)
  10. if n not in lista: lista.append(n)
  11. return lista
  12.  
  13. def desenhaGrafico(x, a, b, c, d, xl="Nº de Elementos", yl="Operações"):
  14. fig = plt.figure(figsize=(10, 10))
  15. ax = fig.add_subplot(111)
  16. plt.plot(x, a, label="bubbleSort")
  17. ax.plot(x, c, label="insertionSort")
  18. ax.plot(x, b, label="selectionSort")
  19. ax.plot(x, d, label="quickSort")
  20. ax.legend(bbox_to_anchor=(1, 1), bbox_transform=plt.gcf().transFigure)
  21. plt.ylabel(yl)
  22. plt.xlabel(xl)
  23. fig.savefig('operacoes.png')
  24.  
  25. def bubbleSort(array):
  26. op = 0
  27. for j in range(0,len(array)):
  28. for i in range(0,len(array)-1):
  29. if array[i]>array[i+1]:
  30. aux = array[i+1]
  31. array[i+1] = array[i]
  32. array[i] = aux
  33. op += 1
  34. return op
  35.  
  36. def selectionSort(array):
  37. op = 0
  38. for j in range(len(array) - 1, 0, -1):
  39. x = 0
  40. for i in range(1, j + 1):
  41. if array[i] > array[x]:
  42. x = i
  43. array[j], array[x] = array[x], array[j]
  44. op += 1
  45. return op
  46.  
  47.  
  48. def insertionSort(array):
  49. op = 0
  50. for i in range(1,len(array)):
  51. x = array[i]
  52. j = i-1
  53. while j>=0 and x<array[j]:
  54. array[j+1] = array[j]
  55. j -= 1
  56. array[j+1] = x
  57. op += 1
  58. return op
  59.  
  60.  
  61. def quickSort(array):
  62. op = 0
  63. aux = []
  64. i = (0, len(array) - 1)
  65. aux.append(i)
  66. for i in aux:
  67. idx = i[0]
  68. temp = i[1]
  69. while temp > idx:
  70. pivo = array[temp]
  71. elem = array[idx]
  72. if elem > pivo:
  73. array[temp] = elem
  74. array[idx] = array[temp - 1]
  75. array[temp - 1] = pivo
  76. temp -= 1
  77. op += 1
  78. else:
  79. idx += 1
  80. esq = i[0]
  81. d = i[1]
  82. if temp > 1 and esq < temp - 1:
  83. aux.append((esq, temp - 1))
  84. if temp < len(array) -1 and temp + 1 < d:
  85. aux.append((temp + 1, d))
  86. return op
  87.  
  88. qntes = []
  89. bubble = []
  90. insertion = []
  91. selection = []
  92. quick = []
  93. for i in range(3000, 24001, 3000):
  94. qntes.append(i)
  95. bubble.append(bubbleSort(geraLista(i)))
  96. insertion.append(insertionSort(geraLista(i)))
  97. selection.append(selectionSort(geraLista(i)))
  98. quick.append(quickSort(geraLista(i)))
  99.  
  100. desenhaGrafico(qntes, bubble, insertion, selection, quick)
Add Comment
Please, Sign In to add comment