Fibonacci Lookup (Hyper-detailed)

Source: Internet
Author: User

    //Fibonacci lookup. CPP    #include <iostream>    #include <string.h>    using namespace STD;Const intMax_size= -;//Fibonacci array length    / * Construct 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]; }/ * Define the Fibonacci lookup method * /     intFibonacci_search (int*a,intNintKey//a is the array to find, n is the length of the array to find, key is the keyword to find{intlow=0;inthigh=n-1;intF[max_size]; Fibonacci (F);//Construct 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));/* strcpy and memcpy have the following 3 main differences. 1, the content of the copy is different.    strcpy can only copy strings, while memcpy copies arbitrary content, such as character arrays, integers, structs, classes, and so on. 2, the method of replication is different. strcpy does not need to specify a length, it encounters the string terminator of the copied character "\" before it ends, so it is prone to overflow.    memcpy determines the length of the copy according to its 3rd parameter. 3, the use of different. strcpy is usually used when copying strings, while other types of data need to be copied, typically memcpy 4, contained in <string.h> 5, function prototypes: void *memcpy (void *dest, const void *SRC, size       _t N); Function: Copies n bytes from the starting position of the memory address referred to by the source SRC to the starting position of the memory address referred to by the target dest */       for(inti=n;i<f[k]-1; ++i) temp[i]=a[n-1];/* First, be clear: if the number of elements of an ordered table is n, and N is exactly (some Fibonacci number-1), i.e., n=f[k]-1, the Fibonacci lookup method can be used. If the element n of an ordered table is not equal to (some Fibonacci number-1), that is, n≠f[k]-1, then the element of the ordered table must be extended to the Fibonacci number that is greater than N-1, which is the code for the For loop: that's the effect. */             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 mid<n indicates that mid is the location to find           Else                 returnN-1;//If mid>=n indicates the value of the extension, return n-1}        }/* The core code of the Fibonacci lookup method is the above paragraph: 1) when the Key=a[mid], the search succeeds, 2) when Key<a[mid], the new look-up range is the first low to the first mid-1, the number of the range is  F[k-1]-1, which is the length of the left of the array, so look in the range [Low, f[k-1]-1], 3) when Key>a[mid], the new look-up range is mid+1 to the first, and the number of ranges is f[k-2]        -1, which is the length of the right side of the array, so look in the [f[k-2]-1] range. 4) For binary lookup, the segmentation is starting from mid= (Low+high)/2, whereas for Fibonacci lookups, the segmentation starts with mid = low + f[k-1]-1; by the above, the number of elements in array A is now f[k]-1, that is, the number of leader is F [K]-1,mid the array into the left and right parts, the left of the length is: f[k-1]-1, then the length of the side is (number leader-left length-1), namely: (F[k]-1)-(F[k-1]-1) = F[k]-  F[K-1]-1 = f[k-2]-1. */      Delete[] temp;//release new out of memory because it is an array, so go to delete [] temp instead of delete temp.      return-1; }intMain () {//int a[] = {0,10,24,35,47,59,62,73,88,99};        inta[]= { to, -, +, +,Panax Notoginseng, About, -, the, the, the, the};intkey= +;intIndex=fibonacci_search (A,sizeof(a)/sizeof(int), key);/* Insert here to discuss the length of the array problem: 1, char a[]= "" type of string, the compiler will be automatically added at the end of the, with sizeof calculation will be calculated as ' 2, the existence of the C language method, such as strlen (s), the calculation of the character The length of the string, where the S pointer. Strlen to calculate the length of a string, you must know where the end is, so use the-s to indicate the end.           Only the character array has the concept of a/s, and the array of other types (int) does not have this concept.        such as int a[]={} knows the array length using sizeof (a)/sizeof (int);            3, then the question came to char a[]={} This type of string, with sizeof calculation will be counted ""?        The answer is no. */        cout<<key<<"is located at:"<<index<<endl; System"PAUSE");return 0; }/* 1, here I first set up an array of 11 elements a[],n=11, then n is located between 8 and 13, that is, f[6] and f[7, so k=7, the number of elements of array A will be expanded Charge, for: f[7]-1 = 12 (here if asked why I want to expand to f[k]-1 instead of f[k], leave a question mark, you can refer to http://www.360doc.com/content/14/0528/10/14505022_3816           53345.shtml) 2, and then based on the core code to find the number: we want to find the element of value 13 Fibonacci series before: 0, 1, 1, 2, 3, 5, 8, 1) mid=low+f[k-1]-1 mid=7;  2) compared to Key<temp[mid], then high=mid-1;  K-=1;           That is, there are high=6,k=6 3) into the cycle, mid=0+5-1=4;  4) compared to Key<temp[mid], then high=mid-1;           K-=1, high=3,k=5 3) into the cycle, mid=0+3-1=2;  4) compared to Key<temp[mid], then high=mid-1;           K-=1, high=1,k=4 5) into the cycle, mid=0+2-1=1;       6) Know mid<n, get position 1 3, the most important thing is to understand the idea of Fibonacci lookup, so that mid remains at the Golden section of the array, like the beginning, the mid front length is f[k-1]-1, the back length is f[k-2]-1, the total length of the array is F[k]-1,mid At the golden dividing point. */  /* vs. Binary lookup comparison: The average performance of the ① Fibonacci lookup is better than binary, ② but the worst-case performance is worse than binary lookup, ③ it has one advantage is that only the addition and subtraction of the partition.    "The obvious advantage of the Fibonacci lookup algorithm compared to binary lookup is that it involves only addition and subtraction operations, not division."  Because the average performance of Fibonacci lookups is better than binary, since Fabiga subtraction takes up more machines. */

Fibonacci Lookup (Hyper-detailed)

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.