Binary Search

Sat 17 May 2025

title: "Binary Search" author: "Rj" date: 2019-04-20 description: "List Test" type: technical_note draft: false


def binary_search(lys, val):  
    first = 0
    last = len(lys)-1
    index = -1
    while (first <= last) and (index == -1):
        mid = (first+last)//2
        if lys[mid] == val:
            index = mid
        else:
            if val<lys[mid]:
                last = mid -1
            else:
                first = mid +1
    return index
list = [1, 2, 5, 6]

print(binary_search(list, 5))
2


Score: 0

Category: basics