|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +/* Interpolation Search: |
| 4 | + Condition: |
| 5 | + array or vector should be sorted of n uniformly distributed value |
| 6 | + * sorted : in a sequence of either increasing or decreasing |
| 7 | + * uniformly distributed : difference between every consecutive element must be same |
| 8 | + * consecutive element difference : arr[8]-arr[7] = arr[2]-arr[1] = .. so on |
| 9 | + |
| 10 | + terminolgy used: |
| 11 | + arr : int arr |
| 12 | + n : given element to be searched |
| 13 | + lo : starting index |
| 14 | + hi : ending index |
| 15 | + |
| 16 | + Logic used: |
| 17 | + * recursion |
| 18 | +*/ |
| 19 | +int interpolationSearch(int arr[], int lo, int hi, int x) |
| 20 | +{ |
| 21 | + int pos; |
| 22 | + if (lo <= hi && x >= arr[lo] && x <= arr[hi]) |
| 23 | + { |
| 24 | + pos = lo + (((double)(hi - lo) / (arr[hi] - arr[lo])) * (x - arr[lo])); |
| 25 | + |
| 26 | + if (arr[pos] == x) |
| 27 | + return pos; |
| 28 | + if (arr[pos] < x) //x is in right sub array as is closer to hi |
| 29 | + return interpolationSearch(arr, pos + 1, hi, x); |
| 30 | + if (arr[pos] > x) //x is in left sub array as is closer to lo |
| 31 | + return interpolationSearch(arr, lo, pos - 1, x); |
| 32 | + } |
| 33 | + return -1; |
| 34 | +} |
| 35 | + |
| 36 | +// Driver Code |
| 37 | +int main() |
| 38 | +{ |
| 39 | + //test cases: |
| 40 | + /* to print the index of the Element searched in the array |
| 41 | + starting from 0 and to print -1 if the element is not |
| 42 | + found. |
| 43 | + |
| 44 | + ** diff : |arr[n]-arr[n-1]| |
| 45 | + */ |
| 46 | + |
| 47 | + //test case 1 : all pos int with diff=1 |
| 48 | + int arr1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}; |
| 49 | + int n = sizeof(arr1) / sizeof(arr1[0]); |
| 50 | + int x = 17; // Element to be searched |
| 51 | + int res = interpolationSearch(arr1, 0, n - 1, x); |
| 52 | + printf("%d\n", res); |
| 53 | + |
| 54 | + //test case 2 : with negative numbers with diff=1 |
| 55 | + int arr2[] = { -5,-4,-3,-2,-1,0,1,2,3,4,5,6,7}; |
| 56 | + n = sizeof(arr2) / sizeof(arr2[0]); |
| 57 | + x = -2; // Element to be searched |
| 58 | + res = interpolationSearch(arr2, 0, n - 1, x); |
| 59 | + printf("%d\n", res); |
| 60 | + |
| 61 | + //test case 3 : diff=5 |
| 62 | + int arr3[] = { -15,-10,-5,0,5,10}; |
| 63 | + n = sizeof(arr3) / sizeof(arr3[0]); |
| 64 | + x = 5; // Element to be searched |
| 65 | + res = interpolationSearch(arr3, 0, n - 1, x); |
| 66 | + printf("%d\n", res); |
| 67 | + |
| 68 | + //test case 4 : element not found |
| 69 | + int arr4[] = { -15,-10,-5,0,5,10,15,20}; |
| 70 | + n = sizeof(arr4) / sizeof(arr4[0]); |
| 71 | + x = -2; // Element to be searched |
| 72 | + res = interpolationSearch(arr4, 0, n - 1, x); |
| 73 | + printf("%d\n", res); |
| 74 | + |
| 75 | + //conditions check: |
| 76 | + |
| 77 | + //test case 5 : unsorted |
| 78 | + // unsorted thus will provide inconsistent solution (may give correct or may not) |
| 79 | + int arr5[] = {1,7,-8, 23, 6}; |
| 80 | + n = sizeof(arr5) / sizeof(arr5[0]); |
| 81 | + x = 6; // Element to be searched |
| 82 | + res = interpolationSearch(arr5, 0, n - 1, x); |
| 83 | + printf("%d\n", res); |
| 84 | + |
| 85 | + //test case 6 : variable difference |
| 86 | + // sorted not uniformly increasing or decreasing thus won't be able to find |
| 87 | + int arr6[] = {1,2,4,7,9,2}; |
| 88 | + n = sizeof(arr6) / sizeof(arr6[0]); |
| 89 | + x = 7; // Element to be searched |
| 90 | + res = interpolationSearch(arr6, 0, n - 1, x); |
| 91 | + printf("%d\n", res); |
| 92 | + |
| 93 | + //test case 7 : diff=0 (same elements) |
| 94 | + // sorted not uniformly increasing or decreasing thus will provide garbage or null value |
| 95 | + int arr7[] = {0,0,0,0,0,0,0,0,0,0,0}; |
| 96 | + n = sizeof(arr7) / sizeof(arr7[0]); |
| 97 | + x = 0; // Element to be searched |
| 98 | + res = interpolationSearch(arr7, 0, n - 1, x); |
| 99 | + printf("%d\n", res); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments