Binary Lookup and Fibonacci lookup

Source: Internet
Author: User

Two-point Search

Description: An array or list of lookups must be ordered, if unordered, sorted first

Complexity: Time complexity O (log2n), Spatial complexity O (n)

C + + source (two versions of recursive and non-recursive)

#include <iostream>using namespace Std;int a[] = {1, 2, 3, 4, 5, 6, 8};int BinarySearch1 (int l, int r, int value) { int mid = (L + R)/2;if (L = = r && a[l]! = value) return-1;if (A[mid] = value) return mid;if (A[mid] > value) r Eturn BinarySearch1 (L, mid-1, value); Elsereturn BinarySearch1 (mid + 1, r, value);} int BinarySearch2 (int value) {int L = 0;int r = sizeof (a)/sizeof (a[0])-1;while (l <= r) {int mid = (L + R)/2;if (a[ MID] = = value) return (L + R)/2;if (A[mid] > value) r = Mid-1;elsel = mid + 1;} return-1;} int main (void) {cout << Binary Search (Recursive) Result: "<< BinarySearch1 (0, sizeof (a)/sizeof (a[0])-1, 5) << Endl; cout << Binary Search (no recursive) Result: "<< BinarySearch2 (4) << Endl;}
Fibonacci Find

Before introducing the Fibonacci lookup algorithm, let's start by introducing a concept that is very closely connected and well known to all-the golden segment.

The golden ratio, also known as the Golden Section, refers to a certain mathematical proportional relationship between parts of things, which is about to be divided into two parts, and the ratio of the majority to the smaller part equals to the ratio of the whole to the larger proportion, which is about 1:0.618 or 1.618:1.

0.618 is recognized as the most aesthetic proportion of the number, the role of this value is not only reflected in such as painting, sculpture, music, architecture and other artistic fields, but also in management, engineering design and other aspects of the role can not be ignored. So it is called the Golden segment.

Do you remember the Fibonacci sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ... (starting with the third number, each of the following numbers is the first two numbers and the same). We will then find that as the Fibonacci sequence increases, the ratio of the two numbers to the front and back is closer to 0.618, and with this feature we can apply the golden ratio to the search technique.

   Basic idea:It is also a lifting algorithm for binary search, which uses the concept of the golden ratio to find the search point in the sequence to improve the search efficiency.  Similarly, Fibonacci lookups belong to an ordered lookup algorithm. Compared to the binary lookup, the key value to be compared with the mid= (Low+high)/2 position of the element comparison, the results are divided into three cases:

1) equal, the element of the mid position is the desired

2) >,low=mid+1;

3) <,high=mid-1.

The Fibonacci lookup is very similar to the binary lookup, which splits the ordered table according to the characteristics of the Fibonacci sequence. He asked for the number of records in the start table to be one Fibonacci number small 1, and 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,high] 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 Fibonacci lookups can be applied recursively.

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 the Fibonacci lookup can be applied recursively.

   complexity analysis: In the worst case, the time complexity is O (log2n), and its expected complexity is O (log2n ).
#include <iostream> #include <vector>using namespace std;const int max_size = 20;int a[] = {1, 5, 15, 22, 25, ----------------};void Fibonacci (int f[]) {f[0] = 0; F[1] = 1;for (size_t i = 2; i < max_size; i++) f[i] = F[i-1] + f[i-2];} int fibonaccisearch (int value) {int f[max_size]; Fibonacci (F); int n = sizeof (a)/sizeof (int), int k = 0;while (n > F[k]-1) k++;vector<int> temp;temp.assign (A, a  + N); for (size_t i = n; i < f[k]-1; i++) Temp.push_back (a[n-1]); int l = 0, r = N-1;while (L <= r) {int mid = L + F[K-1]-1;if (Temp[mid] < value) {L = mid + 1;k = k-2;} else if (Temp[mid] > value) {r = Mid-1;k = K-1;} Else{if (Mid < N) return Mid;elsereturn n-1;}} return-1;} int main () {int index = Fibonaccisearch (n); cout << index << Endl;}

Binary Lookup and Fibonacci lookup

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.