Algorithm Analysis and Implementation of Interpolation Search

Source: Internet
Author: User
Tags random seed

Introduction to Interpolation Search

Interpolation methods include linear interpolation, circular interpolation, parabolic interpolation, and spline interpolation. We use linear interpolation here.

Linear Interpolation (Llne Interpolation) is a common method of Interpolation on the lathe. In this method, Interpolation between two points approaches the point group along a straight line to control the movement of the tool along this line.

For an ordered array, we can use binary lookup to find an element. Here we will introduce another method, Interpolation. Unlike binary search, the algorithm uses an approximate straight line of data distribution to calculate the proportion between the elements to be searched and the elements to be searched.

If the data is evenly distributed, you can use the Interpolation Search Method to search. When there are many objects to search for, the Interpolation Search Method is faster than the binary search method. Some books say that the Interpolation Search Algorithm is faster than the binary search algorithm. I used millions of data tests on the same machine and found that, in fact, this is not the case. Interpolation lookup may converge faster than binary, but multiplication and division are used in its operation. Therefore, the actual result is not faster than binary lookup.
The Interpolation Search Algorithm analyzes the Interpolation Search Method for proportional calculation based on the approximate straight line of data distribution to obtain the intermediate index and compare the data. If the obtained value is smaller than the value to be searched, the lower bound is increased. If the retrieved value is greater than the value to be searched, the lower bound is lowered and the search range is continuously reduced. Therefore, this principle is the same as that of the binary search method, as for the search for the median value, the proportional calculation is adopted. K indicates the object to be searched, while m indicates the possible index value:


Assume that the array is a, the next row is low, and the previous row is high. If the K of the element to be searched, the intermediate value is m.
In binary search, m = low + (high-low)/2;
In Interpolation Search, m = low + (high-low) * (K-a [low])/(a [high]-a [low]).
Interpolation Search Algorithm Implementation (C/OC)

#define MAX 100#define SWAP(x,y) {int t; t = x; x = y; y = t;}

// Main program (C/OC) int number [MAX] = {0}; int I, find; srand (time (NULL )); // generate random seed for (I = 0; I <MAX; I ++) {// generate a random array number [I] = rand () % 100 ;} quicksort (number, 0, MAX-1); // fast sorting, is the order of the array into printf ("series:"); for (I = 0; I <MAX; I ++) printf ("% d", number [I]); find = 11; // The searched number if (I = intsrch (number, find)> = 0) // call the insert sorting method printf ("\ n finds the number in index % d", I); else printf ("\ n cannot find the specified number "); printf ("\ n ");

// Interpolation Search Method int intsrch (int number [], int find) {int low, mid, upper; low = 0; upper = MAX-1; while (low <= upper) {mid = (upper-low) * (find-number [low])/(number [upper]-number [low]) + low; // implement the core algorithm if (mid <low | mid> upper) // return-1 at the end of the query; if (find <number [mid]) upper = mid-1; else if (find> number [mid]) low = mid + 1; else // return mid;} return-1 ;} // fast sorting void quicksort (int number [], int left, int right) {int I, j, k, s; if (left <right) {s = number [(left + right)/2]; // reference the center I = left-1; j = right + 1; while (1) {while (number [++ I] <s); // find while (number [-- j]> s) to the right; // if (I> = j) to the left) break; SWAP (number [I], number [j]);} quicksort (number, left, I-1); // hand back to quicksort (number, j + 1, right); // send back to the right }}


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.