mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-26 07:19:55 +03:00
8d240037b3
1) Easy logic built 2) error minimized: as array is increasing in above example it might not always be increasing.just replace 47 by 2 then run above program it will give an incorrect ans. but in improvised case it is minimized. 3)we are scanning input from user so, more saturated 4)'position' is more reliable than 'index'.
36 lines
749 B
C
36 lines
749 B
C
#include<stdio.h>
|
|
int interpolationSearch(int arr[], int n, int x)
|
|
{
|
|
int q=NULL;
|
|
while(q<n)
|
|
{
|
|
if(arr[q]==x)
|
|
return q;
|
|
q++;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
|
|
int main()
|
|
{
|
|
// Array of items on which search will
|
|
// be conducted.
|
|
int x;
|
|
int arr[] = {10, 12, 13, 16, 18, 19, 20, 21, 22, 23,
|
|
24, 33, 35, 42, 47};
|
|
int n = sizeof(arr)/sizeof(arr[0]); //To get length of an array
|
|
|
|
printf("Enter the no, to be searched");
|
|
scanf("%d",&x); // Element to be searched
|
|
|
|
int index = interpolationSearch(arr, n, x);
|
|
|
|
// If element was found
|
|
if (index != -1)
|
|
printf("Element found at position %d", index+1);
|
|
else
|
|
printf("Element not found.");
|
|
return 0;
|
|
}
|