gpt-4-0314· OpenAI
Arena Human Preference 55K
code in python using binary search
- User
code in python using binary search
- Assistant
Here's a simple implementation of binary search in Python:
pythondef binary_search(arr, target): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if arr[mid] == target: return mid elif arr[mid] < target: low = mid + 1 else: high = mid - 1 return -1 # Example usage: arr = [1, 2, 3, 4, 5, 6, 7, 8, 9] target = 5 result = binary_search(arr, target) if result != -1: print(f"Element found at index {result}") else: print("Element not found in the array.")Please note that the input array must be sorted for the binary search algorithm to work correctly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.