# Interpolation Search in C

In this article, we will discuss the Interpolation search in C with its cases, examples, and outputs.

The Interpolation Search is preferable to Binary Search when the values in a sorted array are evenly distributed. A discrete set of known data points is interpolated to create new data points that fall within its range. Binary search always checks the element in the middle. On the other hand, the value of the key being searched will determine where the interpolation search goes. For instance, the Interpolation search will probably begin its search towards the end side if the key value is closer to the last element.

If an element is present in the array, we must write a C program that uses the Interpolation Search Algorithm to determine its position inside the array of integers.

## Cases of Interpolation Search:

There are several cases of Interpolation Search. These cases are as follows:

### 1. Average Case:

Interpolation searches often perform (log(log n)) comparisons, where n is the total number of elements.

Example:

The intended result will be Position 6 if the input array contains the following data: 1, 2, 3, 4, 5, 6, and the element to be searched is element 6.

The time complexity of the average case is O(log(log n)).

### 2. Best Case:

Interpolation search does just one comparison and returns the position if the object being sought is a middle element of an array.

Example:

The expected output will be Position 3 if the input array contains the data "2, 4, 6, 8, 10," and the element to be searched is "6".The time complexity in the best case is O(1).

Example Code:

Here is the program code for the C program that uses an array of integers to implement interpolation search. Using the GCC compiler, the program is successfully compiled and tested. Also presented below is the output of the program.

### Dispute Resolution

1. Binary search has been improved by interpolation search. Data must be sorted and evenly distributed to use interpolation search.
2. The mid position of the array is determined by the interpolation search using the formula "mid = bottom + (top - bottom) * ((item - a[bottom]) / (a[top] - a[bottom]))".
3. Standard Interpolation Search has an Average Casecase time complexity of O(log(log n)), which demonstrates that it is much faster than Binary and Linear Search.

Code:

Output:

Test case 1:

```Enter total elements (num < 200) :5
Enter 5 Elements in ascending order:
1
2
3
4
5
Search For : 4
Element 4 found at position 4
```

Test case 2:

```Enter total elements (num < 200) :6
Enter 5 Elements in ascending order:
1
2
3
4
5
6
Search For: 2
Element 2 found at position 2
```

### Program Description

1. The data must be sorted and evenly distributed before using interpolation search. Either we sort the array in ascending order, or we must insert elements into the array in ascending order.
2. If we want to do an interpolation search, we divide an array using the mid formula and determine whether the sought-after key value is present in any of the divided portions or not.
3. The algorithm chooses the area to search for by comparing the mid value of the array determined by the formula with the key value that is searched.
4. This process continues, and the top and bottom, or the starting index and ending index of the array inside the definition of the function, keep shifting until we find the element.
5. The crucial aspect of interpolation search is that it should only be used with an equally dispersed and sorted array.