W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
def binarySearch(arr, targetVal): left = 0 right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == targetVal: return mid if arr[mid] < targetVal: left = mid + 1 else: right = mid - 1 return -1 mylist = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] x = 11 result = binarySearch(mylist, x) if result != -1: print("Found at index", result) else: print("Not found")