我在 python 中有一个二进制搜索算法。有一个函数,名为 binary_search(array, value, low, high)
,其中 array
是排序的整数数组,value
是检查数组中存在的对象,low
是左有界索引(初始值 0
),而 high
是右有界索引(初始值 len(array) - 1
)。这是我的代码:
def binary_search(array, value, low, high):
if high < low:
return -1
else:
mid = (low + high)//2
bool_leq = array[mid] < value
bool_geq = array[mid] > value
print("low = %d, high = %d, mid = %d" % (low, high, mid))
print("%s > %s is %r" % (array[mid], value, bool_geq))
print("%s < %s is %r" % (array[mid], value, bool_leq))
if array[mid] > value:
return binary_search(array, value, low, mid-1)
elif array[mid] < value:
return binary_search(array, value, mid+1, high)
else:
return mid
array = []
for i in range(10):
array.append(input())
for i in range(10):
value = input()
answer = binary_search(array, value, 0, 9)
print("%d" % answer)
我做了一个输入数组:[3,5,9,11,14,16,19,23,27,29]
(注意数组长度值只有 10),输出是这样的:
3
low = 0, high = 9, mid = 4
14 > 3 is False
14 < 3 is True
low = 5, high = 9, mid = 7
23 > 3 is False
23 < 3 is True
low = 8, high = 9, mid = 8
27 > 3 is False
27 < 3 is True
low = 9, high = 9, mid = 9
29 > 3 is False
29 < 3 is True
-1
5
low = 0, high = 9, mid = 4
14 > 5 is False
14 < 5 is True
low = 5, high = 9, mid = 7
23 > 5 is False
23 < 5 is True
low = 8, high = 9, mid = 8
27 > 5 is False
27 < 5 is True
low = 9, high = 9, mid = 9
29 > 5 is False
29 < 5 is True
-1
9
low = 0, high = 9, mid = 4
14 > 9 is False
14 < 9 is True
low = 5, high = 9, mid = 7
23 > 9 is False
23 < 9 is True
low = 8, high = 9, mid = 8
27 > 9 is False
27 < 9 is True
low = 9, high = 9, mid = 9
29 > 9 is False
29 < 9 is True
-1
11
low = 0, high = 9, mid = 4
14 > 11 is True
14 < 11 is False
low = 0, high = 3, mid = 1
5 > 11 is True
5 < 11 is False
low = 0, high = 0, mid = 0
3 > 11 is True
3 < 11 is False
-1
14
low = 0, high = 9, mid = 4
14 > 14 is False
14 < 14 is False
4
16
low = 0, high = 9, mid = 4
14 > 16 is False
14 < 16 is True
low = 5, high = 9, mid = 7
23 > 16 is True
23 < 16 is False
low = 5, high = 6, mid = 5
16 > 16 is False
16 < 16 is False
5
19
low = 0, high = 9, mid = 4
14 > 19 is False
14 < 19 is True
low = 5, high = 9, mid = 7
23 > 19 is True
23 < 19 is False
low = 5, high = 6, mid = 5
16 > 19 is False
16 < 19 is True
low = 6, high = 6, mid = 6
19 > 19 is False
19 < 19 is False
6
23
low = 0, high = 9, mid = 4
14 > 23 is False
14 < 23 is True
low = 5, high = 9, mid = 7
23 > 23 is False
23 < 23 is False
7
27
low = 0, high = 9, mid = 4
14 > 27 is False
14 < 27 is True
low = 5, high = 9, mid = 7
23 > 27 is False
23 < 27 is True
low = 8, high = 9, mid = 8
27 > 27 is False
27 < 27 is False
8
29
low = 0, high = 9, mid = 4
14 > 29 is False
14 < 29 is True
low = 5, high = 9, mid = 7
23 > 29 is False
23 < 29 is True
low = 8, high = 9, mid = 8
27 > 29 is False
27 < 29 is True
low = 9, high = 9, mid = 9
29 > 29 is False
29 < 29 is False
9
正如您所看到的,中间数组的左子集(即 [3,5,9,11]
)通常不会比右子集(包括中间,即 [14,16,19,23,27,29]
)创建输出。错误是什么?
答案 0 :(得分:3)
对于您的问题,这是一个致命的赠品:
14 < 3 is True
您存储的是字符串而不是数字(按字典顺序,"14"
小于 "3"
)。
我建议您将输入语句更改为:
array.append(int(input())) # try/except might be nice as well.
而且,作为旁注:除非您对 Python 3.6 之前的版本感到震惊,否则我会转向 f-strings。它们是一种更好的格式化方式:
# Change:
print("Answer is %d" % answer)
# into:
print(f"Answer is {answer}")
您可以只在您的特定情况下使用 print(answer)
,因为这就是您要打印的全部内容。这个建议更多地用于格式化其他而不是单个对象。
答案 1 :(得分:2)
您正在错误地构建数组和测试值:
array = []
for i in range(10):
array.append(input())
for i in range(10):
value = input()
answer = binary_search(array, value, 0, 9)
print("%d" % answer)
您的 array
是一个字符串值列表,不一定正确排序(尤其是当您输入数字字符串并希望它们遵循数字比较规则时)。
使用有效的 (int) 输入测试您的 binary_search
函数会产生正确的结果,因此函数本身看起来不错:
>>> binary_search([3,5,9,11,14,16,19,23,27,29], 3, 0, 9)
low = 0, high = 9, mid = 4
14 > 3 is True
14 < 3 is False
low = 0, high = 3, mid = 1
5 > 3 is True
5 < 3 is False
low = 0, high = 0, mid = 0
3 > 3 is False
3 < 3 is False
0
尝试使用它来构建您的数组并测试值:
array = sorted(int(input()) for _ in range(10))
for _ in range(10):
print(binary_search(array, int(input()), 0, 9)
答案 2 :(得分:0)
您所比较的确实是整数,但与您的想法不同。
Python 中的str
字符根据 Unicode 标准进行编码,该标准明确地为每个不同的字符分配一个数字(代码点)。
您可以使用 ord("char")
轻松检查字符代码点。
本质上,当操作数为 str
类型时,逻辑运算符所做的是比较在两个字符串中相同位置找到的第一个不同字符的实际代码点。
例如:
"3" > "11" == True
因为:
ord("3") > ord("1") == True
因此,由于 input()
无法解析您的输入并自动将其转换为预期的数据类型(幸运的是我会这么说),您必须明确地执行此操作:
value = int(input())