Advertisement
KaySawbridge

Binary Search Function

Aug 8th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.48 KB | None | 0 0
  1. from random import randint
  2.  
  3. sorted_list = [i + randint(0,9) for i in range(0,100,10)]
  4. print (sorted_list)
  5. def binary_search(sorted_list, value):
  6.     left = 0
  7.     right = len(sorted_list) - 1
  8.     while left <= right:
  9.         mid = int((left + right)/2)
  10.         if sorted_list[mid] > value:
  11.             right = mid - 1
  12.         elif sorted_list[mid] < value:
  13.             left = mid + 1
  14.         else:
  15.             return mid
  16.     return False
  17.    
  18. print(binary_search(sorted_list,96))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement