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.

Source Code

def binary_search(L, data):
    first = 0
    last = len(L)-1
    while(first<=last):
        mid = (first+last)//2
        if L[mid]==data:
            return mid
        elif L[mid]>data:
            first=mid+1
        else:
            last=mid-1
    return -1


L = [90,78,25,13,9,2]
print(L)
index = binary_search(L,2)
if index == -1:
    print('Element not found')
else:
    print('Elemenent found at position',index+1)

Output

[90, 78, 25, 13, 9, 2]
Elemenent found at position 6