//binary Search by mam
//Find an element in an array
#include<stdio.h>
int main()
{
int a[50],n,loc=-1, key, beg,last,mid,i;
printf("Enter number of array elements : ");
scanf("%d",&n);
printf("\nEnter array elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEntered elements are : ");
for(i=0;i<n;i++){
printf("%d ",i);
}
beg=0;
last=n-1;
printf("\nEnter integer value to search in sorted array:");
scanf( "%d", &key );
while(beg<=last)//Loop will run until unless only one element is not remaining
{
mid = (beg + last) / 2; // determine index of middle element
if(a[mid]==key)
{
loc=mid; //save the location of element.
break;
}
else if(a[mid]>key) //Middle element is greater than key
{
last=mid-1;//If middle element is greater than key, we need to search left subarray
}
else if(a[mid]<key) //Middle element is less than key
{
beg=mid+1;//If middle element is less than key, we need to search right subarray
}
}
if(loc!=-1)
{
printf("\nElement found at index %d", loc+1);//Location is exact position, not index
}
else
{
printf("\nElement not found!");
}
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: