PHP ordered table search-Fibonacci search

Source: Internet
Author: User
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 )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.