二进制搜索算法不起作用
Binary Search Algorithm is not working
所以我写了一个二进制搜索算法,但是当我测试时运行它不能完美地工作。
这是代码
def binarySearch(lst, target):
low = 0
high = len(lst)-1
while high >= low:
mid = (high + low)//2
if target < lst[mid]:
high = mid - 1
elif target > lst[mid]:
low = mid + 1
else:
return mid
return (-1 * (mid+1))
abd这里是调用函数的代码
lst_test = [3, 4, 6, 7]
target_values = [1, 3, 5, 8]
for t in target_values:
i = binarySearch(lst_test, t)
if (i < 0):
print("In", lst_test, t, "is going to be inserted at index",-1*(i+1))
lst_test.insert((i+1)*-1, t)
else:
print("In", lst_test, t, "was found at index", i)
print("The final list is:", lst_test)
问题是这样的,我想将列表 target_values 添加到第一个正确的顺序中,而实际上 运行 它给出的函数
In [3, 4, 6, 7] 1 is going to be inserted at index 0
In [1, 3, 4, 6, 7] 3 was found at index 1
In [1, 3, 4, 6, 7] 5 is going to be inserted at index 3
In [1, 3, 4, 5, 6, 7] 8 is going to be inserted at index 5
The final list is: [1, 3, 4, 5, 6, 8, 7]
这很奇怪,它可以正常工作,但仅在调用的最后一部分失败
有什么办法可以解决这个问题吗?最终列表应为 [1,3,4,5,6,7,8]
根据要求,我跟踪了我的二进制搜索算法,它的质量很差。我希望这会有所帮助
Mid point is: 1
target value is smaller than a mid point
Mid point is: 0
target value is smaller than a mid point
In [3, 4, 6, 7] 1 is going to be inserted at index 0
Mid point is: 2
target value is smaller than a mid point
Mid point is: 0
target value is larger than a mid point
Mid point is: 1
Found the index location at 1
In [1, 3, 4, 6, 7] 3 was found at index 1
Mid point is: 2
target value is larger than a mid point
Mid point is: 3
target value is smaller than a mid point
In [1, 3, 4, 6, 7] 5 is going to be inserted at index 3
Mid point is: 2
target value is larger than a mid point
Mid point is: 4
target value is larger than a mid point
Mid point is: 5
target value is larger than a mid point
In [1, 3, 4, 5, 6, 7] 8 is going to be inserted at index 5
The final list is: [1, 3, 4, 5, 6, 8, 7]
我想我明白了。放入我推荐的打印语句。尤其要跟踪现有列表末尾的插入。我相信您会发现您无法将 low 驱动得足够高以引起在列表末尾插入;您最多只能在 before 最后一个元素插入,这正是您的测试中发生的事情。
只需将函数更改为 return (-1 * (low+1))
即可:
def binarySearch(lst, target):
low = 0
high = len(lst)-1
while high >= low:
mid = (high + low)//2
if target < lst[mid]:
high = mid - 1
elif target > lst[mid]:
low = mid + 1
else:
return mid
return (-1 * (low+1))
输出:
('In', [3, 4, 6, 7], 1, 'is going to be inserted at index', 0)
('In', [1, 3, 4, 6, 7], 3, 'was found at index', 1)
('In', [1, 3, 4, 6, 7], 5, 'is going to be inserted at index', 3)
('In', [1, 3, 4, 5, 6, 7], 8, 'is going to be inserted at index', 6)
('The final list is:', [1, 3, 4, 5, 6, 7, 8])
原始实现的问题是代码假设 mid
是插入索引,但它永远不会超出循环中的当前列表,因为当值被插入到列表末尾时它应该如此。
所以我写了一个二进制搜索算法,但是当我测试时运行它不能完美地工作。
这是代码
def binarySearch(lst, target):
low = 0
high = len(lst)-1
while high >= low:
mid = (high + low)//2
if target < lst[mid]:
high = mid - 1
elif target > lst[mid]:
low = mid + 1
else:
return mid
return (-1 * (mid+1))
abd这里是调用函数的代码
lst_test = [3, 4, 6, 7]
target_values = [1, 3, 5, 8]
for t in target_values:
i = binarySearch(lst_test, t)
if (i < 0):
print("In", lst_test, t, "is going to be inserted at index",-1*(i+1))
lst_test.insert((i+1)*-1, t)
else:
print("In", lst_test, t, "was found at index", i)
print("The final list is:", lst_test)
问题是这样的,我想将列表 target_values 添加到第一个正确的顺序中,而实际上 运行 它给出的函数
In [3, 4, 6, 7] 1 is going to be inserted at index 0
In [1, 3, 4, 6, 7] 3 was found at index 1
In [1, 3, 4, 6, 7] 5 is going to be inserted at index 3
In [1, 3, 4, 5, 6, 7] 8 is going to be inserted at index 5
The final list is: [1, 3, 4, 5, 6, 8, 7]
这很奇怪,它可以正常工作,但仅在调用的最后一部分失败 有什么办法可以解决这个问题吗?最终列表应为 [1,3,4,5,6,7,8]
根据要求,我跟踪了我的二进制搜索算法,它的质量很差。我希望这会有所帮助
Mid point is: 1
target value is smaller than a mid point
Mid point is: 0
target value is smaller than a mid point
In [3, 4, 6, 7] 1 is going to be inserted at index 0
Mid point is: 2
target value is smaller than a mid point
Mid point is: 0
target value is larger than a mid point
Mid point is: 1
Found the index location at 1
In [1, 3, 4, 6, 7] 3 was found at index 1
Mid point is: 2
target value is larger than a mid point
Mid point is: 3
target value is smaller than a mid point
In [1, 3, 4, 6, 7] 5 is going to be inserted at index 3
Mid point is: 2
target value is larger than a mid point
Mid point is: 4
target value is larger than a mid point
Mid point is: 5
target value is larger than a mid point
In [1, 3, 4, 5, 6, 7] 8 is going to be inserted at index 5
The final list is: [1, 3, 4, 5, 6, 8, 7]
我想我明白了。放入我推荐的打印语句。尤其要跟踪现有列表末尾的插入。我相信您会发现您无法将 low 驱动得足够高以引起在列表末尾插入;您最多只能在 before 最后一个元素插入,这正是您的测试中发生的事情。
只需将函数更改为 return (-1 * (low+1))
即可:
def binarySearch(lst, target):
low = 0
high = len(lst)-1
while high >= low:
mid = (high + low)//2
if target < lst[mid]:
high = mid - 1
elif target > lst[mid]:
low = mid + 1
else:
return mid
return (-1 * (low+1))
输出:
('In', [3, 4, 6, 7], 1, 'is going to be inserted at index', 0)
('In', [1, 3, 4, 6, 7], 3, 'was found at index', 1)
('In', [1, 3, 4, 6, 7], 5, 'is going to be inserted at index', 3)
('In', [1, 3, 4, 5, 6, 7], 8, 'is going to be inserted at index', 6)
('The final list is:', [1, 3, 4, 5, 6, 7, 8])
原始实现的问题是代码假设 mid
是插入索引,但它永远不会超出循环中的当前列表,因为当值被插入到列表末尾时它应该如此。