Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- sorted_list =[randint(0,100) for i in range(1,21)]
- sorted_list = sorted(sorted_list)
- sorted_list =[2, 5, 12, 12, 12, 35, 36, 47, 53, 54, 57, 66, 69, 72, 75, 78, 86, 86, 97, 98]
- def binary_search(sorted_list, value):
- left = 0
- right = len(sorted_list) - 1
- while left <= right:
- mid = int((right + left)/2)
- if sorted_list[mid] > value:
- right = mid - 1
- elif sorted_list[mid] < value:
- left = mid + 1
- else:
- return mid
- return False
- print("Value is at position", binary_search(sorted_list, 12))
Advertisement
Add Comment
Please, Sign In to add comment