Objective:
In the previous we introduced two-point lookup, interpolation lookup. The interpolation lookup is an improvement on the binary lookup. Similarly, the protagonist of this blog, Fibonacci Lookup, is also an improvement on binary search (using the Golden Section principle).
Because of this process analysis compared to the previous complex, we can Baidu.
Code:
<?php//Fibonacci Find using the Golden Section principle//algorithm core://1, when $num== $arr [$mid], find success//2, when $num < $arr [$mid], the new range is $low to $mid-1, this time the number of the FBI ($k-1)-1//2, when $num > $arr [$mid], the new range is $mid+1 to $high, at this time the number of the FBI ($k-2)-1 $i = 0; Number of times to store comparisons//In order to implement the algorithm, we first prepare a Fibonacci sequence//@func generate Fibonacci sequence//@param sequence length function Fbi ($i) {if ($i < 2) {return ($i = = 0 ? 0:1); } return FBI ($i-1) + FBI ($i-2);} @param the array to be found//@param the number function Fbisearch (array $arr, $num) {$count = count ($arr) to be searched; $lower = 0; $high = $count-1; $k = 0; Global $i; Calculates the position of the $count in the Fibonacci sequence while ($count > (Fbi ($k)-1)) {$k + +; }//The value of the discontent is complete, the value of the complement to the last of the array for ($j = $count; $j < Fbi ($k)-1; $j + +) {$arr [$j] = $arr [$count-1]; }//Find start while ($lower <= $high) {$i + +; Calculates the currently delimited subscript $mid = $lower + Fbi ($k-1)-1; if ($num < $arr [$mid]) {$high = $mid-1; $k = $k-1; Fibonacci Sequence number subscript minus one bit}else if ($num > $arr [$mid]) {$lower =$mid + 1; $k = $k-2; Fibonacci Sequence number subscript minus two-bit}else{if ($mid <= $count-1) {return $mid; }else{return $count -1;//here $mid greater than $count-1 description is the completion value, return $count-1}}} return-1;} $arr = Array (0,1,16,24,35,47,59,62,73,88,99), $pos = Fbisearch ($arr), Echo $pos. " <br> "; Echo $i;
The above is the PHP ordered table lookup----Fibonacci Find content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!