简体   繁体   English

二元搜索的比较次数

[英]number of comparisons of binary search

What is the total number of comparisons necessary to locate all the n sorted distinct integers in an array using binary search? 使用二分查找定位数组中所有n个排序的不同整数所需的比较总数是多少? I think the number is n log 2 n (2 is the base), but I am not sure. 我认为这个数字是n log 2 n (2是基数),但我不确定。 What do you think? 你怎么看?

If you want an exact answer, then it is clearly not N log(N) or N log 2 (N). 如果你想要一个确切的答案,那么它显然不是 N log(N)或N log 2 (N)。 For most integers N, logN and log 2 are not rational, but the number of comparisons must be an integer value. 对于大多数整数N,logN和log 2是不合理的,但比较的数量必须是整数值。

Also, the exact answer will depend on implementation details of the binary search algorithm. 此外,确切的答案将取决于二进制搜索算法的实现细节。 For example, if a "comparison" is a simple relation that returns true and false, more comparisons are required than when a "comparison" returns negative, zero or positive. 例如,如果“比较”是返回true和false的简单关系,则需要比“比较”返回负数,零或正数时更多的比较。 (In the latter case, you can short circuit when the algorithm hits the key early.) (在后一种情况下,当算法提前击中键时,您可以短路。)

找到每一个需要log2 n ,它需要完成n次,所以n log n就是这样。

I would say it takes n log m 我会说它需要n log m

Where m is the size of the array, so log m to find a value. 其中m是数组的大小,所以log m来查找值。 And then you do this n times for the n distinct integers. 然后你为n不同的整数做了n次。

So n log m in total. 所以n log m总计。

There will be at most (2 * log 2 n + 1) rounded down(so 7.6 => 7) comparisons for 1 number. 对于1个数字,将最多(2 * log 2 n + 1)向下舍入(因此7.6 => 7)比较。

When we land on some number in array, first we check if it is the one we are looking for. 当我们在阵列中的某个数字上着陆时,首先我们检查它是否是我们正在寻找的那个。 (== first comparison). (==第一次比较)。 After that we check if it smaller (or greater) (second comparison). 之后我们检查它是否更小(或更大)(第二次比较)。

To find the number, we must process at most log 2 n numbers. 要查找数字,我们必须处理最多log 2 n个数字。

And we must make the last comparison on the last number, to check that this is the one. 我们必须对最后一个数字进行最后一次比较,以确定这是一个。

So looking for 16 in [1..16] will take 2*log 2 16 + 1 = 9 comparisons (assuming we land on these numbers: 8, 12, 14, 15, 16). 因此在[1..16]中寻找16将采用2 * log 2 16 + 1 = 9比较(假设我们落在这些数字上:8,12,14,15,16)。 And looking for 10 in [1..10] will take 2*log 2 10 + 1 = 7.6 => 7 (Assuming we land on these numbers: 5, 8, 9, 10). 在[1..10]中寻找10将需要2 * log 2 10 + 1 = 7.6 => 7(假设我们登陆这些数字:5,8,9,10)。

So for n numbers there will be at most n times more. 因此对于n个数字,最多将是n倍。

Thanks for your comments, now I am clear. 谢谢你的评论,现在我很清楚。 I think what Stephen C said is true. 我认为斯蒂芬C所说的是真的。 I think we cannot actually work out a formula for this question unless we have a exact value. 我认为除非我们有确切的价值,否则我们实际上无法计算出这个问题的公式。 However, if the n=2^n -1,like 511(2^9 -1), it is easy to compute. 但是,如果n = 2 ^ n -1,如511(2 ^ 9 -1),则很容易计算。 For 511, total number of comparisons = 1x1 + 2X2 + 3X4 + 4X8 + 5X16 + 6X32 + 7X64 + 8X128 + 9X256 = 4097. 对于511,比较总数= 1x1 + 2X2 + 3X4 + 4X8 + 5X16 + 6X32 + 7X64 + 8X128 + 9X256 = 4097。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM