Search Algorithms


In [1]:
import numpy as np

In [42]:
arr = np.random.random(100)
arr = np.sort(arr)

# find 8

def binary_search(array, value):
    """Find the value in the array using binary search."""
    arr = array
    res = 0
    while True:
        idx = len(arr)//2
        if value > arr[idx]:
            arr = arr[idx:]
            res += idx
        elif value < arr[idx]:
            arr = arr[:idx]
        elif value == arr[idx]:
            return idx + res

print(binary_search(arr,arr[10]))


10