Fibonacci Search (Fibonacci Search) is a method of finding using the golden partitioning principle.
The core of Fibonacci lookup is:
1. When key = = A[mid], the search is successful;
2. When key < A[mid], the new search range is low to mid-1, at this time the number of ranges is f[k-1]-1, that is, the length of the left of the array;
3. When key < A[mid], the new search range is mid+1 to high, at this time the number of ranges is f[k-2]-1, that is, the length of the right of the array;
ImportRandom#source is the array to look for, key is the number to finddefFibonaccisearch (source,key):#generate the Fibonacci seriesFIB = [0,1] forIinchRange (1,36): Fib.append (fib[-1]+fib[-2]) #determine where to find the array in the Fibonacci sequenceK =0 N=Len (source)#n>fib[k]-1 Here is not a profound #If n happens to be an item on the Fibonacci, and the element to be searched is exactly the last, the array length must be filled to the number of the next item in the sequence. while(N > Fib[k]-1): K= k + 1#fills the array you want to find to the specified length forIinchRange (N,fib[k]): A.append (a[-1]) Low,high= 0,n-1 while(Low <=High ):#get the Golden split position element subscriptMID = low + fib[k-1]-1if(Key <A[mid]):#if key is smaller than this element, the key value should be between low and mid-1, and the remainder of the range is F (k-1)-1High = Mid-1k= K-1elif(Key >A[mid]):#if key is larger than this element, then the key should be between mid+1 to high and the number of elements left is f (k)-F (k-1) -1=f (k-2)-1Low = mid + 1k= K-2Else: if(Mid <N):returnMidElse: returnN-1return-1## # function Test # # ##generate an array to findA = [Random.randint (1,100000) forXinchRange (0,33)]a.append (673990) A.sort ()#number to findKey = 673990#output the location to locate the subscriptPrint(Fibonaccisearch (A,key))
Novice Python, please correct me in the wrong place.
Fibonacci Lookup-Python implementation