Mancolo

Untitled

Dec 7th, 2020
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. from random import randint
  2. import time
  3.  
  4. N = int(input("N: "))
  5. A = []
  6.  
  7. for i in range(N):
  8.     A.append(randint(0, 1000))
  9.  
  10. A = sorted(A)
  11. print(A)
  12.  
  13. X = int(input("X: "))
  14.  
  15. time1 = time.perf_counter_ns()
  16.  
  17. q1 = False
  18. for i in A:
  19.     if X == i:
  20.         q1 = True
  21.         break
  22. print(q1)
  23.  
  24. time2 = time.perf_counter_ns()
  25. print("Время работы линейного алгоритма: {}".format(time2-time1))
  26.  
  27. sr = 0
  28. end = len(A)-1
  29. q1 = False
  30.  
  31. while sr != end:
  32.     mid = (sr+end)//2
  33.     m = A[(sr+end)//2]
  34.  
  35.     if m == X:
  36.        print(True)
  37.        break
  38.     elif m > X:
  39.         end = mid
  40.     else:
  41.         st = mid + 1
  42. else:
  43.     print(False)
  44.  
  45. time3 = time.perf_counter_ns()
  46. print("Время работы бинарного алгоритма: {}".format(time3-time2))
  47.  
Add Comment
Please, Sign In to add comment