This commit is contained in:
2025-08-05 09:19:34 +08:00
commit 584548d006
1696 changed files with 53855 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
array = [1, 2, 3, 4, 5, 6, 7, 8, 100]
def binary_search(low, high, target_num):
mid = (low + high) // 2
if low > high:
return None # 如果找不到数字,返回None
if target_num > array[mid]:
return binary_search(mid + 1, high, target_num) # 向右查找
elif target_num < array[mid]:
return binary_search(low, mid - 1, target_num) # 向左查找
else:
print(target_num, mid)
return mid # 找到数字,返回其索引
# 使用修改后的函数查找数字
result = binary_search(0, len(array)-1, 3)
print(result if result is not None else "Number not found")