vojta249

binary_search

Nov 2nd, 2023
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.45 KB | None | 0 0
  1. def binary_search(searched_value, ordered_list):
  2.     top_value = len(ordered_list)-1
  3.     bottom_value = 0
  4.     guess = (bottom_value+top_value)//2
  5.     while top_value != bottom_value:
  6.         if (searched_value < ordered_list[guess]):
  7.             top_value = guess
  8.         elif (searched_value > ordered_list[guess]):
  9.             bottom_value = guess
  10.         else:
  11.             return True
  12.         guess = (top_value + bottom_value) // 2
  13.     return False
Advertisement
Add Comment
Please, Sign In to add comment