我在CS课的介绍中,得到了执行递归二进制搜索的任务,该搜索将在列表中显示所搜索项目的索引,否则它将返回负数。
对该函数进行分类的代码如下:(Python代码)
numList = [1,2,3,4,5,...6,7,8,9]
key = <insert number to search for here>
position = binarySearch(numList, key)
print("Your number is at " + str(position))
不允许修改函数的参数。
给定
def binarySearch(numList, key):
<code here>
关于如何完成此操作的任何提示?我似乎总是无法恢复原始数字的索引。二进制搜索必须是递归的。