# C program to Implement Interpolation Search on Array of Integers

«
»

This is a C Program to implement interpolation search on array of integers.

Problem Description

We have to create a C Program which finds the position of an element in an array of numbers using Interpolation Search Algorithm, if it is present in the array.

Expected Input and Output

1. Average case : On an average interpolation search makes a total of (log(log n)) comparisons where n is the total number of elements. For example:

```If the input array has the data as {1, 2, 3, 4, 5, 6}
and the element to be searched is 6,
then the expected output will be Position 6```

Average case time complexity : O(log(log n))

2. Best case : If the item to be searched is a middle element of an array, interpolation search makes just one comparison and returns the position. For example:

Note: Join free Sanfoundry classes at Telegram or Youtube
```If the input array has the data as {2, 4, 6, 8, 10}
and the element to be searched is 6,
then the expected output will be Position 3```

Best case time complexity : O(1)

Program/Source Code

Here is source code of the C Program to implement Interpolation Search on array of integers. The program is successfully compiled and tested using Codeblocks gnu/GCC compiler on Windows 10. The program output is also shown below.

Problem Solution

1. Interpolation search is an improved variant of binary search. To apply interpolation search the data should be in sorted and equally distributed form.
2. Interpolation search uses this formula to find out the mid position of the array “mid = bottom + (top – bottom) * ((item – a[bottom]) / (a[top] – a[bottom]))“.
3. Average Case time complexity of Interpolation Search is O(log(log n)), which proves that it is way faster than Binary and Linear Search.

1. `/*`
2. ` * C Program for Interpolation search algorithm`
3. ` */`
4. `#include <stdio.h>`
5. `#define MAX 200`
6. `/* Interpolation search function */`
7. `int interpolation_search(int a[], int bottom, int top, int item)`
8. `{`
9. `    int mid;`
10. `    while (bottom <= top) {`
11. `        mid = bottom + (top - bottom)*((item-a[bottom])/(a[top]-a[bottom]));`
12. `        if (item == a[mid])`
13. `            return mid + 1;`
14. `        if (item < a[mid])`
15. `            top = mid - 1;`
16. `        else`
17. `            bottom = mid + 1;`
18. `    }`
19. `    return -1;`
20. `}`
21. `/* End of interpolation_search() */`
22. `/* The main() begins */`
23. `int main()`
24. `{`
25. `    int arr[MAX];`
26. `    int i, num;`
27. `    int item, pos;`
28. `    printf("\nEnter total elements (num < %d) :", MAX);`
29. `    scanf("%d", &num);`
30. `    printf("Enter %d Elements in ascending order:\n",num);`
31. `    for (i = 0; i < num; i++)`
32. `        scanf("%d", &arr[i]);`
33. `    printf("\nSearch For : ");`
34. `    scanf("%d", &item);`
35. `    pos = interpolation_search(&arr, 0, num - 1, item);`
36. `    if (pos == -1)`
37. `        printf("\nElement %d not found\n", item);`
38. `    else`
39. `        printf("\nElement %d found at position %d\n", item, pos);`
40. `    return 0;`
41. `}`
Program Explanation

1. To apply interpolation search the data should be in sorted and equally distributed form. Either we have to insert elements in ascending order, or we can sort the array in ascending order.
2. In interpolation search we divide the array using the formula for mid and check whether the key value that is searched for is present in any of the divided parts or not.
3. On comparing the mid value of the array calculated from formula and the key value that is searched, the algorithm decides which part to start searching.
4. It continues like that and the values of top and bottom i.e. the starting index and the ending index of the array inside function definition keep on changing till we locate the element.
5. The important thing about interpolation search is that it should be applied upon a sorted and equally distributed array only.

Runtime Test Cases
```1. Enter total elements (num < 200) :6
Enter 6 Elements in ascending order:
1
2
3
4
5
6

Search For : 6

Element 6 found at position 6

2. Enter total elements (num < 200) :5
Enter 5 Elements in ascending order:
2
4
6
8
10

Search For : 6

Element 6 found at position 3```

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms. 