Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- balls=[40,43,56,67,72,76,210,218,223,239]
- tennisball=67
- def binary_search(sorted_list,value):
- movect = 1
- left =0
- right = len(sorted_list)-1
- while left <= right :
- mid = int((left + right)/2)
- if sorted_list[mid]<value:
- left = mid+1
- movect += 1
- elif sorted_list[mid]>value:
- right = mid-1
- movect += 1
- else:
- return mid, movect
- return False
- pos,mov2=binary_search(balls,tennisball)
- print('The tennis ball is the ', str(pos+1),' ball in the shelf.')
- print('binary search took ' ,str(mov2),'times to move')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement