In this program, we are going to share Interpolation search program in c programming language with the output. If you are a beginner and want to start learning the c programming, then keep your close attention in this tutorial as I am going to share a C program to interpolation search with the output.
Copy the below C program and execute it with the help of C compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include<stdio.h> int interpolationSearch(int arr[], int n, int x) { int lo = 0, hi = (n - 1); while (lo <= hi && x >= arr[lo] && x <= arr[hi]) { int pos = lo + (((double)(hi-lo) / (arr[hi]-arr[lo]))*(x - arr[lo])); if (arr[pos] == x) return pos; if (arr[pos] < x) lo = pos + 1; else hi = pos - 1; } return -1; } int main() { int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23, 24, 33, 35, 42, 47}; int n = sizeof(arr)/sizeof(arr[0]); int x = 19; int index = interpolationSearch(arr, n, x); if (index != -1) { printf("Element found at index %d", index); } else{ printf("Element not found."); return 0; } } |
Element found at index 5
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: C program to Implement Interpolation Search, c programs, c programs with output, c programs with solutions, interpolation program in c, Interpolation Search, interpolation search example, interpolation search time complexity, robust interpolation search