Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. def creare_vector(size, max):
  2. from random import randint
  3. return [randint (0, max) for _ in range(size)]
  4.  
  5. def bubble(v):
  6. if len(v) >= 50000:
  7. print("Nu se poate efectua Bubble Sort pentru atat de multe numere")
  8. else:
  9. for i in range(0,len(v)):
  10. for j in range (0, len(v)-1):
  11. if v[j] > v[j+1]:
  12. aux = v[j]
  13. v[j] = v[j+1]
  14. v[j+1] = aux
  15. return v
  16.  
  17. def count(v):
  18. if v != []:
  19. if max(v) >= 10**6:
  20. print("Nu se poate efectua Count Sort pentru numere de dimensiuni atat de mari")
  21. else:
  22. a = [0]*(max(v)+1)
  23. v2 = []
  24. for i in v:
  25. a[i] += 1
  26. for i in range(max(v)+1):
  27. for j in range(1, a[i]+1):
  28. v2 = v2 + [i]
  29. return v2
  30. else: return v
  31.  
  32. def radix(A):
  33. max1 = max(A)
  34. nr = 0
  35. while max1 != 0:
  36. nr = nr + 1
  37. max1 = max1 // 10
  38. for p in (10 ** k for k in range(0, nr)):
  39. v = [0] * 10
  40. B = [0] * len(A)
  41. for i in range(len(A)):
  42. v[(A[i] // p) % 10] += 1
  43. for i in range(1, 10):
  44. v[i] = v[i] + v[i - 1]
  45. for i in range(len(A) - 1, -1, -1):
  46. v[(A[i] // p) % 10] -= 1
  47. B[v[(A[i] // p) % 10]] = A[i]
  48. A = B
  49. return A
  50.  
  51. def merge(a,b):
  52. c = []
  53. i,j = 0,0
  54. while i < len(a) and j < len(b):
  55. if a[i] < b[j]:
  56. c.append(a[i])
  57. i += 1
  58. else:
  59. c.append(b[j])
  60. j += 1
  61. if i == len(a):
  62. c.extend(b[j:])
  63. else:
  64. c.extend(a[i:])
  65. return c
  66.  
  67. def merge_sort(a):
  68. if len(a) <= 1:
  69. return a
  70. left,right = merge_sort(a[:len(a)//2]), merge_sort(a[len(a)//2:])
  71. return merge(left,right)
  72.  
  73. def quick_sort(a):
  74. from random import randint
  75. if len(a) <= 1:
  76. return a
  77. lower, equal, greater = [], [], []
  78. pivot = a[randint(0, len(a)-1)]
  79. for x in a:
  80. if x < pivot:
  81. lower.append(x)
  82. elif x == pivot:
  83. equal.append(x)
  84. else: greater.append(x)
  85. return quick_sort(lower) + equal + quick_sort(greater)
  86.  
  87. def testare(v):
  88. if (v == None):
  89. return "NU"
  90. ok = 1
  91. for i in range(0,len(v)-1):
  92. if v[i] > v[i+1]:
  93. ok = 0
  94. if ok == 0:
  95. return "NU"
  96. else: return "DA"
  97.  
  98. q = open("TESTARI.txt", 'r')
  99. n = int(q.readline())
  100. from time import time
  101. l = q.readlines()
  102. for x in l:
  103. x = x.split()
  104. x = [int(j) for j in x]
  105. v = creare_vector(x[0],x[1])
  106. print("N = " + str(x[0]) + ", Max = " + str(x[1]))
  107. cv = list()
  108. for index in range(len(v)):
  109. cv.append(v[index])
  110. t0 = time()
  111. cv = bubble(cv)
  112. t1 = time()
  113. calificativ = testare(cv)
  114. print("BubbleSort: ", str(t1-t0), calificativ)
  115.  
  116. cv = list()
  117. for index in range(len(v)):
  118. cv.append(v[index])
  119. t0 = time()
  120. cv = count(cv)
  121. t1 = time()
  122. calificativ = testare(cv)
  123. print("CountSort: ", str(t1-t0), calificativ)
  124.  
  125. cv = list()
  126. for index in range(len(v)):
  127. cv.append(v[index])
  128. t0 = time()
  129. cv = radix(cv)
  130. t1 = time()
  131. calificativ = testare(cv)
  132. print("RadixSort: ", str(t1-t0), calificativ)
  133.  
  134. cv = list()
  135. for index in range(len(v)):
  136. cv.append(v[index])
  137. t0 = time()
  138. cv = merge_sort(cv)
  139. t1 = time()
  140. calificativ = testare(cv)
  141. print("MergeSort: ", str(t1-t0), calificativ)
  142.  
  143. cv = list()
  144. for index in range(len(v)):
  145. cv.append(v[index])
  146. t0 = time()
  147. cv = quick_sort(cv)
  148. t1 = time()
  149. calificativ = testare(cv)
  150. print("QuickSort: ", str(t1-t0), calificativ)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement