# Title: Binary search
# Heading: Python Program for Binary Search
# Level: intermediate
# Tags: searching, binary search, divide and conquer, sorted list
# Time: O(log n)
# Space: O(1)
# About: Finds a value in a sorted list by checking the middle item and halving the part still to search at every step.

def binary_search(items, target):
    """The index of target in the sorted list items, or -1."""
    low, high = 0, len(items) - 1
    while low <= high:
        middle = (low + high) // 2
        if items[middle] == target:
            return middle
        if items[middle] < target:
            low = middle + 1   # target is in the right half
        else:
            high = middle - 1  # target is in the left half
    return -1


numbers = [3, 8, 12, 17, 23, 31, 42, 56, 64, 79]
print("Index of 23:", binary_search(numbers, 23))
print("Index of 50:", binary_search(numbers, 50))
