We have introduced binary search and interpolation search. The interpolation search improves binary search. In the same way, Fibonacci search, the main character of this blog, is also an improvement in binary search (using the golden splitting principle ). Preface:
We have introduced binary search and interpolation search. The interpolation search improves binary search. In the same way, Fibonacci search, the main character of this blog, is also an improvement in binary search (using the golden splitting principle ).
Because this process is more complex than before, you can use Baidu.
Code:
$ Arr [$ mid], the new range is $ mid + 1 to $ high, the number of ranges is Fbi ($ K-2)-1 $ I = 0; // store the number of comparisons // to implement this algorithm, we need to first prepare a Fibonacci series // @ func to generate the Fibonacci series // @ param series length function Fbi ($ I) {if ($ I <2) {return ($ I = 0? 0: 1);} return Fbi ($ I-1) + Fbi ($ I-2 );} // @ param: array to be searched // @ param: number to be searched function fbisearch (array $ arr, $ num) {$ count = count ($ arr); $ lower = 0; $ high = $ count-1; $ k = 0; global $ I; // calculates $ count at the position of the Fibonacci series while ($ count> (Fbi ($ k) -1) {$ k ++;} // complete the incomplete values. The value is the last bit of the array for ($ j = $ count; $ j <Fbi ($ k)-1; $ j ++) {$ arr [$ j] = $ arr [$ count-1];} // start searching while ($ lower <= $ high) {$ I ++; // calculate the subscript currently separated by $ mid = $ lower + Fbi ($ k-1) -1; if ($ num <$ arr [$ mid]) {$ high = $ mid-1; $ k = $ k-1; // the subscripts of the Fibonacci series minus one digit} else if ($ num> $ arr [$ mid]) {$ lower = $ mid + 1; $ k = $ k-2; // the subscripts of the Fibonacci series minus two digits} else {if ($ mid <= $ count-1) {return $ mid;} else {return $ count-1; // here, $ mid is greater than $ count-1, indicating a completion value. $ count-1 }}return-1 ;}$ arr = array, 59,62, 99); $ pos = fbisearch ($ arr, 62); echo $ pos."
"; Echo $ I;
The above is the PHP ordered table search-Fibonacci search content. For more information, see PHP Chinese network (www.php1.cn )!