#include<stdio.h>
main()
{
int a[50],i,n,key;
printf("Enter the size of the array:\n");
scanf("%d",&n);
printf("Enter elements into the array in ascending order:");
for(i=0; i<n; i++) {
scanf("%d",&a[i]);
}
printf("The sorted array is: ");
for(i=0; i<n; i++) {
printf("%d\t",a[i]);
}
printf("\nEnter the element to be searched: ");
scanf("%d",&key);
int start=0;
int end=n-1;
while(start<=end)
{
int m=(a[end]-a[start])/(end-start);
int Pos=start+(key-a[start])/m;
if(a[Pos]==key)
{
printf("%d found at %d",key,Pos+1);
break;
}
else if(a[Pos]>key)
{
end=Pos-1;
}
else
{
start=Pos+1;
}
printf("Element Not Found!!");
break;
}
}
Note: Need to be arranged in compiler after copied
OutPut:
Enter the size of the array:
4
Enter elements into the array in ascending order:1
2
5
7
The sorted array is: 1 2 5 7
Enter the element to be searched: 5
5 found at 3