Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. # coding: utf-8
  2.  
  3. # In[10]:
  4.  
  5.  
  6. ##quicksort without separating equal items
  7.  
  8. import timeit
  9. import random
  10.  
  11. eps = 1e-16
  12. N = 2
  13. locations = [0.0, 0.5, 1.0 - eps]
  14.  
  15.  
  16. def median(x1, x2, x3):
  17. if (x1 < x2 < x3) or (x3 < x2 < x1):
  18. return x2
  19. elif (x1 < x3 < x2) or (x2 < x3 < x1):
  20. return x3
  21. else:
  22. return x1
  23.  
  24. def qsort(lst):
  25. indices = [(0, len(lst))]
  26.  
  27. while indices:
  28. (frm, to) = indices.pop()
  29. if frm >= to:
  30. print(frm, to)
  31. print(indices)
  32. continue
  33.  
  34. # Find the partition:
  35. N = to - frm
  36. inds = [frm + int(N * n) for n in locations]
  37. values = [lst[ind] for ind in inds]
  38. partition = median(*values)
  39.  
  40. # Split into lists:
  41. lower = [a for a in lst[frm:to] if a <= partition]
  42. upper = [a for a in lst[frm:to] if a > partition]
  43. #counts = sum([1 for a in lst[frm:to] if a == partition])
  44.  
  45. ind1 = frm + len(lower)
  46. ind2 = to - len(upper)
  47.  
  48. # Push back into correct place:
  49. lst[frm:ind1] = lower
  50. #lst[ind1:ind2] = [partition] * counts
  51. lst[ind2:to] = upper
  52.  
  53. # Enqueue other locations
  54. if frm < ind1:
  55. indices.append((frm, ind1))
  56. if ind2 < to:
  57. indices.append((ind2, to))
  58. print(indices)
  59. return lst
  60.  
  61.  
  62. def randomized_quicksort():
  63. lst = [i for i in range(N)]
  64. random.shuffle(lst)
  65. return qsort(lst)
  66.  
  67.  
  68. def test_quicksort():
  69. lst = randomized_quicksort()
  70. assert (lst == [i for i in range(N)])
  71.  
  72.  
  73. # Is our algorithm correct
  74. test_quicksort()
  75.  
  76. # How fast is our algorithm
  77. print(timeit.timeit(randomized_quicksort, number=1))
  78.  
  79.  
  80. # In[13]:
  81.  
  82.  
  83. ##quicksort using first element as partition
  84.  
  85. import timeit
  86. import random
  87.  
  88. eps = 1e-16
  89. N = 10000
  90. locations = [0.0, 0.5, 1.0 - eps]
  91.  
  92.  
  93.  
  94. def qsort(lst):
  95. indices = [(0, len(lst))]
  96.  
  97. while indices:
  98. (frm, to) = indices.pop()
  99. if frm == to:
  100. continue
  101.  
  102. # Find the partition:
  103. partition = lst[0]
  104.  
  105. # Split into lists:
  106. lower = [a for a in lst[frm:to] if a < partition]
  107. upper = [a for a in lst[frm:to] if a > partition]
  108. counts = sum([1 for a in lst[frm:to] if a == partition])
  109.  
  110. ind1 = frm + len(lower)
  111. ind2 = ind1 + counts
  112.  
  113. # Push back into correct place:
  114. lst[frm:ind1] = lower
  115. lst[ind1:ind2] = [partition] * counts
  116. lst[ind2:to] = upper
  117.  
  118. # Enqueue other locations
  119. indices.append((frm, ind1))
  120. indices.append((ind2, to))
  121. print(indices)
  122. return lst
  123.  
  124.  
  125. def randomized_quicksort():
  126. lst = [i for i in range(N)]
  127. random.shuffle(lst)
  128. return qsort(lst)
  129.  
  130.  
  131. def test_quicksort():
  132. lst = randomized_quicksort()
  133. assert (lst == [i for i in range(N)])
  134.  
  135.  
  136. # Is our algorithm correct
  137. test_quicksort()
  138.  
  139. # How fast is our algorithm
  140. print(timeit.timeit(randomized_quicksort, number=1))
  141.  
  142.  
  143. # In[17]:
  144.  
  145.  
  146. ##recursive quicksort
  147.  
  148. import timeit
  149. import random
  150.  
  151. eps = 1e-16
  152. N = 10000
  153. locations = [0.0, 0.5, 1.0 - eps]
  154.  
  155.  
  156. def median(x1, x2, x3):
  157. if (x1 < x2 < x3) or (x3 < x2 < x1):
  158. return x2
  159. elif (x1 < x3 < x2) or (x2 < x3 < x1):
  160. return x3
  161. else:
  162. return x1
  163.  
  164. def qsort(lst, frm, to):
  165.  
  166. if frm < to:
  167. # Find the partition:
  168. N = to - frm
  169. inds = [frm + int(N * n) for n in locations]
  170. values = [lst[ind] for ind in inds]
  171. partition = median(*values)
  172.  
  173. # Split into lists:
  174. lower = [a for a in lst[frm:to] if a < partition]
  175. upper = [a for a in lst[frm:to] if a > partition]
  176. counts = sum([1 for a in lst[frm:to] if a == partition])
  177.  
  178. ind1 = frm + len(lower)
  179. ind2 = ind1 + counts
  180.  
  181. # Push back into correct place:
  182. lst[frm:ind1] = lower
  183. lst[ind1:ind2] = [partition] * counts
  184. lst[ind2:to] = upper
  185.  
  186. # recursively call function on partitioned lists
  187. qsort(lst[frm:ind1], frm, ind1)
  188. qsort(lst[ind2:to], ind2, to)
  189. return lst
  190.  
  191.  
  192. def randomized_quicksort():
  193. lst = [i for i in range(N)]
  194. random.shuffle(lst)
  195. return qsort(lst, 0, len(lst) - 1)
  196.  
  197.  
  198. def test_quicksort():
  199. lst = randomized_quicksort()
  200. assert (lst == [i for i in range(N)])
  201.  
  202.  
  203. # Is our algorithm correct
  204. test_quicksort()
  205.  
  206. # How fast is our algorithm
  207. print(timeit.timeit(randomized_quicksort, number=1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement