Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- import time
- N = int(input("N: "))
- A = []
- for i in range(N):
- A.append(randint(0, 1000))
- A = sorted(A)
- print(A)
- X = int(input("X: "))
- time1 = time.perf_counter_ns()
- q1 = False
- for i in A:
- if X == i:
- q1 = True
- break
- print(q1)
- time2 = time.perf_counter_ns()
- print("Время работы линейного алгоритма: {}".format(time2-time1))
- sr = 0
- end = len(A)-1
- q1 = False
- while sr != end:
- mid = (sr+end)//2
- m = A[(sr+end)//2]
- if m == X:
- print(True)
- break
- elif m > X:
- end = mid
- else:
- st = mid + 1
- else:
- print(False)
- time3 = time.perf_counter_ns()
- print("Время работы бинарного алгоритма: {}".format(time3-time2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement