实现算法?

Implementing an algorithm?

我要写一个小程序来实现下面的算法:

Assume you have a search algorithm which, at each level of recursion, excludes half of the data from consideration when searching for a specific data item. Search stops only when one data item is left. How many levels of recursion are required when the number of elements in the data is 1024?

有没有人知道如何分析或有任何关于如何开始的建议?

您需要找到 d 的最小值,使得:

1 * 2 * 2 * 2 * .... * 2    = 1024
    ____________________
    total of d times

上面是对的,因为每次乘以2实际上是递归向上一层,你从1元素的停止子句向上,直到得到初始数据大小,即1024。

上面的等式其实就是2^d = 1024

从两边提取log_2很容易解决:

log_2(2^d) = log^2(1024)
d = 10

P.S。请注意,以上是递归调用的次数,不包括初始调用,因此对该方法的调用总数为 d+1=11,1 次来自调用环境,10 次来自方法本身。