chatgpt-4o-latest-20250326· OpenAI
LMArena Human Preference 140K
how do i write binary search in python?
- User
how do i write binary search in python?
- Assistant
Binary search is an efficient algorithm for finding an item from a sorted list. It works by repeatedly dividing the search interval in half.
Here’s how to implement binary search in Python:
✅ Iterative Version
def binary_search(arr, target): left, right = 0, len(arr) - 1 while left <= right: mid = (left + right) // 2 # Find the middle index if arr[mid] == target: return mid # Target found elif arr[mid] < target: left = mid + 1 # Search in the right half else: right = mid - 1 # Search in the left half return -1 # Target not found
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.