def binary_search(array, val):
    """Binary search."""
    sorted_array = sorted(array)
    i = 0
    j = len(array) - 1
    while i <= j:
        mid = (i + j) // 2
        if sorted_array[mid] == val:
            return mid
        if sorted_array[mid] < val:
            i = mid + 1
        else:
            j = mid - 1