返回索引的递归二进制搜索

问题描述 投票:0回答:1

我在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>

关于如何完成此操作的任何提示?我似乎总是无法恢复原始数字的索引。二进制搜索必须是递归的。

python recursion binary-search
1个回答
0
投票
geeksforgeeks复制的二进制搜索
© www.soinside.com 2019 - 2024. All rights reserved.