Base algorithm-Lookup: Interpolation lookup

Source: Internet
Author: User

Algorithm description

Let's start with a practical question:
We look for the word "worst" in an English-Chinese dictionary, we will never imitate half-lookup (or Fibonacci lookup), look for the elements in the middle of the dictionary first, and then look up the three elements of the dictionary four and so on. In fact, We started looking around the desired address (in the back of the dictionary), and we call this lookup the interpolation lookup.

As can be seen, interpolation looks different from the previous discussion of the search algorithm, the previous lookup algorithm is based on strict comparison, that is to say we have no knowledge of the distribution of elements in the linear table (or there is no heuristic information). In practice, however, many of the tables involved in finding problems meet some statistical characteristics.

Interpolation lookups generally satisfy two hypothetical conditions when used in practice:

(1) Each access to the data is quite expensive compared to the usual instructions. For example, the table you are looking for must be on disk rather than in memory, so disk access is made for each comparison.

(2) The data is not only ordered, but also exhibits uniform distribution characteristics.

Pseudo code

Insertvalue_search (r,n,element)//realization of a given ordered uniform distribution array r element in the lookup//input: Array r, array length n, the location of the element to be found//There are no elements to find from←0, to←n-1                    Initialize start and end position while (from <= to) middle←from+ (Element-r[from])/(R[to]-r[from]) * (to-from+0.5)//interpolation method to calculate if r[mid]= Element  return midif r[mid]>element  to←mid-1if r[mid]<element  from←mid+1return-1

Specific implementation

/** function: This function is used to implement interpolation lookup algorithm * Input: Find array search_table, array length n, find element element* output: Returns the position of the element */int insertvalue_search (int search_table[], int n, int element) {int low = 0;int High = n-1;while (Low <= high) {int mid = (int.) (Low + (Element-search_table[low])/( Search_table[high]-search_table[low]) * (high-low)); if (search_table[mid] = = Element) {return mid;} else if (Search_table[mid] > Element) {high = mid-1;} Else{low = mid + 1;}} whilereturn-1;}

In terms of time complexity, the time complexity of interpolation lookups is also O (Logn), but the average performance of the interpolation algorithm is much better than that of binary lookups for larger table lengths and more uniform keyword distributions. However, if the distributions in the array are similar to the extremely uneven data [0,1,2,2000,2001,......, 99998,99999], it is not necessarily an appropriate algorithm to find the interpolation.

So, there is no best algorithm, only the most appropriate algorithm for the specific situation.

Base algorithm-Lookup: Interpolation 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.