1.

For a given list of values in descending order, write a method in Python to search for a value with the help of Binary search method. The method should return position of the value and should return -1 if the value not present in the list.

Answer»

def binarysrch (nums, x):

high=len (nums)

low=0

while low < high:

mid=(low+high)/2

midval=nums [mid]

if midval > x:

low = mid + 1

elif midval < x:

high = mid

else:

return mid

return -1



Discussion

No Comment Found