Recently saw a requirement to use addition subtraction only to achieve the two-point search, Baidu a bit, the original to use a called Fibonacci search algorithm. Check Baidu, that is to say:
The Fibonacci lookup is very similar to the binary lookup, which splits the ordered table according to the characteristics of the Fibonacci sequence. He requested that the number of records in the start table be a small 1 of the Fibonacci number, i.e. n=f (k)-1;
The comparison between the K value and the record of the position F (k-1) (Mid=low+f (k-1)-1) is started, and the results are divided into three types.
1) equal, the element of the mid position is the desired
2) >, low=mid+1,k-=2; Description: Low=mid+1 indicates that the element to be found is within the [mid+1,hign] range, the number of elements in the k-=2 description range [Mid+1,high] is n (F (k-1)) = Fk-1-f (k-1) =fk-f (k-1) -1=f (k-2)-1, So it's possible to recursively apply Fibonacci lookups
3) <, high=mid-1,k-=1; Description: Low=mid+1 indicates that the element to be found is within the [low,mid-1] range, the number of elements in the k-=1 description range [Low,mid-1] is F (k-1)-1, so you can recursively apply Fibonacci lookups
Most of the instructions omit a description of the condition:n=f (k)-1, the number of records in the table is 1 smaller than one Fibonacci number. What is this for?
I thought for a long time, finally found that the reason is actually very simple:
is for the unification of the format, in order to facilitate the writing of recursive or cyclic programs. The data in the table is f (k)-1, split with the mid value and one for the other, then F (k)-2 is left. Just give two sub-sequences, the number of each subsequence is F (k-1)-1 and F (k-2)-1, the format is consistent with the previous one. Otherwise, the number of elements per subsequence may be f (k-1), F (k-1) -1,f (K-2), F (k-2)-1, and writing a program can be cumbersome.
The implementation code is as follows:
//Fibonacci find. cpp#include"stdafx.h"#include<memory>#include<iostream>using namespacestd; Const intMax_size= -;//the length of the Fibonacci array /*constructs a Fibonacci array*/ voidFibonacci (int*F) {f[0]=0; f[1]=1; for(intI=2; i<max_size;++i) f[i]=f[i-1]+f[i-2]; } /*Defining the Fibonacci lookup method*/ intFibonacci_search (int*a,intNintKey//A is the array to find, n is the length of the array to find, and key is the keyword to find{ intlow=0; inthigh=n-1; intF[max_size]; Fibonacci (F);//constructs a Fibonacci array F intk=0; while(n>f[k]-1)//calculates the position of N in the Fibonacci sequence++K; int* TEMP;//extend array A to the length of the f[k]-1temp=New int[f[k]-1]; memcpy (Temp,a,n*sizeof(int)); for(inti=n;i<f[k]-1;++i) temp[i]=a[n-1]; while(low<=High ) { intmid=low+f[k-1]-1; if(key<Temp[mid]) { High=mid-1; K-=1; } Else if(key>Temp[mid]) { Low=mid+1; K-=2; } Else { if(mid<N)returnMid//if equal, mid is the location to find it Else returnN-1;//If the mid>=n is an extended value, return n-1 } } Delete[] temp; return-1; } int_tmain (intARGC, _tchar*argv[]) { intA[] = {0, -, -, *, -, -, +, the, the, About}; intkey= -; intIndex=fibonacci_search (A,sizeof(a)/sizeof(int), key); cout<<key<<"Is located at:"<<index; System ("PAUSE"); return 0; }
A detailed explanation and implementation of the Fibonacci lookup principle