我正在编写一些代码来查找最后一个键,其值不超过PHP的给定整数 例如,阵列(0 => 1,1 => 2,2 => 3,3 => 3,4 = 1→4)。给定整数3,我将找到密钥3.(二进制搜索)
我在互联网上找了一些关于二元搜索的参考文献 我找到了这个,就是找到第一个键,其值不小于C ++的给定整数 它说:
template <class _ForwardIter, class _Tp, class _Distance>
_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,
const _Tp& __val, _Distance*)
{
_Distance __len = 0;
distance(__first, __last, __len);
_Distance __half;
_ForwardIter __middle;
while (__len > 0) {
__half = __len >> 1;
__middle = __first;
advance(__middle, __half);
if (*__middle < __val) {
__first = __middle;
++__first;
__len = __len - __half - 1;
}
else
__len = __half; // <======this line
}
return __first;
}
那么,为什么要使用&#34; __ len = __half;&#34;而不是&#34; __ len = __half + 1;&#34;?
赢得了关键/价值,这是&#34; _中间&#34;在每个循环中,在这个二元搜索过程中被遗忘并迷失?
我的意思是,看起来这两个&#34; __ len&#34;&#t;&nbsp;&#39;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; >
PS: 我的原始问题的PHP代码是:
$cid_start = $count - 1;
$len = $count;
while($len > 0){
$half = $len >> 1;
$middle = $cid_start - $half;
if($c_index[$middle][1] > $time_start){
$cid_start = $middle - 1;
$len = len - $half - 1;
}else{
$len = $half + 1;
}
}
会起作用吗?还是会犯错? 当我在数组中找不到任何内容时,如何得到-1或者其他东西?
答案 0 :(得分:0)
回答&#34;为什么不......?&#34;问题:此算法的工作方式略有不同。如果下限实际上是第一个元素,我们就会遇到问题。
__len
将被缩减,直到它的2:
while (__len > 0)
{
__half = __len >> 1; // __half is __len/2 floored
__middle = __first + __half; // Assuming random access iterators, simplified
if (*__middle < __val) // Lower bound is __first, so *__middle >= __val
{
// […]
}
else
__len = __half + 1; // .. self-explanatory
}
,然后我们得到一个无限循环。即,__len == 2
:
while (__len > 0) // Okay, 2 > 0
{
__half = __len >> 1; // __half is now 2 >> 1, which is 1
__middle = __first + __half; // Rewritten for clarity. __middle == __first.
if (*__middle < __val) // lower bound is at __first, so *__middle >= __val
{
// […]
}
else
__len = __half + 1; // __len is 1 + 1 == 2
} // Endless loop
如果指定的长度为__half
本身,则不会发生这种情况 - 然后对于__len == 2
我们得到1,对于__len == 1
我们得到0。
答案 1 :(得分:0)
二进制搜索的算法非常简单。
/**
* Search $value in $array
* Return the position in $array when found, -1 when not found
* $array has numeric consecutive keys (0..count($array)-1)
* $array is sorted ascending; this condition is mandatory for binary search
* if $array is not sorted => the output is rubbish
*/
function search($value, array $array)
{
// At each step search between positions $start and $end (including both)
$start = 0;
$end = count($array) - 1;
// End when the search interval shrunk to nothing
while ($start <= $end) {
// Get the middle of the interval
// This is shorter and faster than intval(($start + $end) / 2)
$middle = ($start + $end) >> 1;
// Check the value in the middle of the current search interval
if ($value == $array[$middle]) {
// Found
return $middle;
}
// Not found yet; the binary step: choose a direction
if ($value < $array[$middle]) {
// Search in the left half
$end = $middle - 1;
} else {
// Search in the right half
$start = $middle + 1;
}
}
// Not found
return -1;
}