binary search

mail@pastecode.io avatar
unknown
python
a year ago
500 B
0
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