PHP ordered table lookup----Fibonacci Lookup

Source: Internet
Author: User

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.


<?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 (!

  • 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: 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.