Page title

Section title

def binary_search(arr, target):
    # Initialize the left and right pointers
    left = 0
    right = len(arr) - 1

    # While there is a range to search
    while left <= right:
        # Calculate the middle index
        mid = (left + right) // 2

        # Check if the middle element is the target
        if arr[mid] == target:
            return mid  # Target found, return index

        # If the target is greater, ignore the left half
        if arr[mid] < target:
            left = mid + 1
        # If the target is smaller, ignore the right half
        else:
            right = mid - 1

    # Target not found, return -1
    return -1

# Example usage:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
index = binary_search(numbers, target)
print(f'Index of {target}:', index)  # Output: Index of 7: 6