更多关于二元搜索

时间:2016-06-07 19:20:48

标签: search binary-search

我刚学会二元搜索。我搜索了它的用途。

我发现的其他事情:

最后三个*我不太了解。有人可以解释一下吗?

此外,还有其他用途吗?

1 个答案:

答案 0 :(得分:1)

二进制搜索具有O(logn)复杂度....这意味着如果有一个' n'排序后的元素,需要logn比较来搜索数字。 例如,您的列表包含20个整数:{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}。 .. 并且您想要搜索此列表中的任何数字(想想此列表中的数字)。现在看看这些问题:

Q1. Is 10 the number which you chose? - (y/n)?
-------- if yes then done. (I say "No")
if no then,
Q2. Is the number less than 10 - (y/n)?
-------- I say "No"; which means the number is greater than 10
Q3. Is 15 the number which you chose? - (y/n)?
-------- if yes then done. (I say "No")
Q4. Is the number less than 15? - (y/n)?
-------- I say "Yes"; So now its sure that the number is in between 10 and 15 (i.e., [11,14])
Q5. Is 12 the number which you chose? - (y/n)?
-------- if yes then done. (I say "No")
Q6. Is the number greater than 12? - (y/n)?
I say "Yes"; So now its sure that the number is either 13 or 14
Q7. Is the number 13? - (y/n)?
-------- I say "Yes" else its 14.
(Actually I had chosen 13 as my number)

So in total there are 20 elements, log of 20 (base 2) is 4.32 ~ 4.
So here are my comparisons:
1 st comparison: Is the number less than 10 - (y/n)?
2 nd comparison: Is the number less than 15? - (y/n)?
3 rd comparison: Is the number greater than 12? - (y/n)?
4 th comparison: Is the number 13? - (y/n)?

我在4次比较中找到了数字13(或14),这是正确的,因为logn = log20(base 2)= 4.

因此,Akinator应用程序的工作原理如下。它适用于大量数据,并根据用户对Akinator问题的回复预测答案。 阅读决策树和二进制搜索树可能会帮助您:)