binary search

mail@pastecode.io avatar
unknown
python
2 years ago
500 B
1
Indexable
Never
def binary_search(num, array):

    array = sorted(array)

    while True:

        value = False

        middle_index = int(len(array) / 2)

        if num == array[middle_index]:
            value = True
            break

        elif num > array[middle_index] and num <= array[-1]:
                array = array[middle_index:]

        elif num < array[middle_index] and num >= array[0]:
                array = array[:middle_index]


        else: break

    return value