Fibonacci Lookup Method

Source: Internet
Author: User

  1. void Fibonacci (int *f)
  2. {
  3. F[0] = 1;
  4. F[1] = 1;
  5. For (int i = 2; i < MAXSIZE; i++)
  6. {
  7. F[i] = F[i-1] + f[i-2];
  8. }
  9. }
  10. int Fibonacci_search (int *a, int n, int key)
  11. {
  12. int Low, high, mid;
  13. low = 1;
  14. High = n-1;
  15. int k = 0;
  16. int f[maxsize];
  17. Fibonacci (F);
  18. //Question one: This finds the position of N in the Fibonacci sequence, why is f[k]-1, not f[k]?
  19. While (n > F[k]-1)
  20. {
  21. k++;
  22. }
  23. //Question two:
  24. //This place, I found that the length of the array a looked for is not good, for example, I am now looking for 31 in the position of the array a
  25. //Then, because n = 13, is located between the 7th number (21) and 8th number (34) in the Fibonacci sequence, so K's
  26. //value is 7,f[k]-1 equals 20, then the length of array a needs to be a[20]. Change the number again, I don't know this
  27. //How should I control it?
  28. For (int i = n; i < f[k]-1; i++)
  29. {
  30. A[i] = A[high];
  31. }
  32. //Question three:
  33. //And this judgment, when the key value is less than a[mid], in [Low, f[k-1]-1] in the range of search
  34. //When the key value is greater than a[mid], search within the range of [f[k-2]-1], what is the basis?
  35. While (Low <= high)
  36. {
  37. MID = low + f[k-1]-1;
  38. if (Key < A[mid])
  39. {
  40. High = mid-1;
  41. K = k-1;
  42. }
  43. Else if (key > A[mid])
  44. {
  45. Low = mid + 1;
  46. K = k-2;
  47. }
  48. Else
  49. {
  50. if (mid <= High)
  51. {
  52. return mid;
  53. }
  54. Else
  55. return n;
  56. }
  57. }
  58. return-1;
  59. }
  60. Analytical:
  61. The first thing to 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), i.e. n≠f[k]-1, then the element of the ordered table must be extended to the Fibonacci number of 1 that is greater than N, this code:
  62. for (int i = n; i < f[k]-1; i++)
  63. {
  64. A[i] = A[high];
  65. }
  66. Is this role.
  67. Answer below
  68. The first question: Read the above and you should know why ① is. Find the position of N in the Fibonacci sequence, why F[k]-1, not f[k], because the Fibonacci lookup method is determined by f[k]-1, not f[k]. If you don't understand for a moment, keep looking.
  69. The second question: the length of a is actually very good estimates, such as you define a 10 elements of the ordered array a[20],n=10, then N is located in 8 and 13, that is, f[6] and f[7], so k=7, at this time the number of elements of the array A is expanded to: f[7] 1 = 12; As you define a b[20], and B has 12 elements, that is, n=12, then very good, n = f[7]-1 = 12, do not need to expand, or n=8 or 9 or 11, it will be expanded to 12, and then as you cite the example, n=13, and finally arrive at N at 13 and 21, that is f[7] and F[8], at this time k=8, then f[8]-1 = 20, array A will have 20 elements. So, n = x (13<=x& lt;=20), the last is to be expanded to 20, if n=25, then the number of elements of array A must be expanded to 34-1 = 33 (25 bits 21 and 34, that is f[8] and f[9), at this time k=9,f[9]- 1 = 33), so when n = x (21<=x<=33), the last is expanded to 33. That is to say, the number of elements in the last array must be (some Fibonacci number-1), which is the beginning of the relationship between N and F[k]-1.
  70. The third problem: for binary lookups, the segmentation begins with mid= (Low+high)/2, whereas for Fibonacci lookups, the segmentation starts with mid = low + f[k-1]-1, and by the above, the number of elements in array A is now f[k]-1, that is, the number of leaders is f[k]- 1,mid the array into the left and right parts, the left side of the length is: f[k-1]-1, then the length of the side is (number leader-the length of the left-1), namely: (F[k]-1)-(F[k-1]-1) = F[k]-f[k-1]-1 = f[k-2]-1.
  71. The core of Fibonacci lookup is:
  72. 1) when Key=a[mid], find success;
  73. 2) when Key<a[mid], the new search range is the first low to the mid-1, at this time the number of the range is f[k-1]-1, that is, the length of the left of the array, so in [Low, f[k-1]-1] within the scope of the search;
  74. 3) when Key>a[mid], the new lookup range is mid+1 to high, at this time the number of ranges is f[k-2]-1, which is the length of the right of the array, so look in the [f[k-2]-1] range.

Fibonacci Lookup Method

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.