Skip to main content
CodeCatch Logo

197

Users

504

Snippets

86

Languages

Unlimited

Uploads

Compile

CodeCatch supports code compiling for 15+ languages such as JavaScript, Python, C, Go, and more. Simply click the "Run" button when viewing a post to see the compiled output.

# Python binary search function
def binary_search(arr, target):
    left = 0
    right = len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

# Usage
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 7
result = binary_search(arr, target)
if result != -1:
    print(f"Element is present at index {result}")
else:
    print("Element is not present in array")
12345678910111213141516171819