Ordered table lookup algorithm (binary, interpolation, Fibonacci)

Source: Internet
Author: User

Today, we summarize the three kinds of algorithms commonly used in order table search to share with you.

1. Binary Search
Binary find also called binary search, its basic idea is: in the ordered table, take the intermediate record as the comparison object, if the equality is found successful; If the given value is less than the middle record of the keyword, then in the middle of the left half of the record to continue to find, if the given value is greater than the median, then the second half of the process

Algorithm code (Java Edition)

intBinarySearch (int[] A,intkey) {        intLow,high,mid; Low= 0; High= A.length-1;  while(low<=High ) {Mid= (Low+high)/2; if(key<A[mid]) high= Mid-1; Else if(key>A[mid]) Low= Mid+1; Else                 returnmid; }        return-1;}

The time complexity is O (Logn), which is obviously better than the time complexity O (n) of sequential lookups.

2. Interpolation Lookup

Binary lookups are also noticeably inadequate in some cases, and finding in an array of evenly sized distributions can be a waste of time for binary lookups.
Binary Find in mid= (low+high)/2 = low+ (high-low)/2;
The algorithm scientists modified the equation to replace 1/2 with (Key-a[low])/(A[high]-a[low]), and then there was mid = low+ (high-low) * (Key-a[low])/(A[high]-a[low]);

Algorithm code (Java Edition)

 Public intInsertsearch (int[] A,intkey) {        intLow,high,mid; Low= 0; High= A.length-1;  while(low<=High ) {Mid= low+ (high-low) * (Key-a[low])/(a[high]-A[low]); if(key<A[mid]) high= Mid-1; Else if(key>A[mid]) Low= Mid+1; Else                 returnmid; }        return-1;}

The time complexity of interpolation lookups is also O (Logn), but the average performance is much better for lookup tables where the keyword is evenly distributed than binary.

3. Fibonacci Lookup

Find with the Golden segment
The core idea is to find success when Key=a[mid], when Key<a[mid], the new range is the first low to ground mid-1, when the number of ranges is f[k-1]-1, when Key>a[mid], the new range is the first low to ground mid+1, The number of scopes is f[k-2]-1 at this time.

Algorithm instance (Java Edition)

 Public intFibonaccisearch (int[] A,intkey) {        intLow,high,mid,k=0; Low=0; High=a.length;  while(A.length>fibonacci (k)-1) K++;  for(intI=a.length;i<fibonacci (k) -1;i++) A[i]= A[a.length-1];  while(low<=High ) {Mid= Low+fibonacci (k-1)-1; if(key<A[mid]) { High= Mid-1; K=k-1; }            Else if(key>A[mid]) { Low=mid+1; K=k-2; }            Else{                if(mid<=a.length)returnmid; Else                     returna.length; }        }        return-1; }         Public intFibonacciintN) {        returnN>2?fibonacci (n-1) +fibonacci (n-2): 1; }

The time complexity of Fibonacci lookups is also O (Logn), but in terms of average performance, it is better than binary lookups. The worst case scenario, such as Key=1 here, is always on the left in the lookup, then the lookup is less efficient than the binary lookup.

Ordered table lookup algorithm (binary, interpolation, Fibonacci)

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.