Code:
#include<stdio.h>void main(){
int a[100],i,n,mid,high,low,search;
printf("enter length of array : ");
scanf("%d",&n);
printf("enter element in array in assending order: ");
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
}
printf("the arrray which you enterd :");
for(i=1;i<=n;i++){
printf("%d ",a[i]);
}
printf("\nenter a element which you want to search : ");
scanf("%d",&search);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high){
if(a[mid]<search)
low=low+1;
else if(a[mid]==search){
printf("the element is found %d at location %d : ",search,mid);
break;
}
else
high=mid-1;
mid=(low+high)/2;
}
if(low>high)
printf("%d element is not present in the array",search);
}
Comments
Post a Comment