#include <stdio.h>
void main()
{
int a[10],beg,mid,end,n,find,i,label=0;
printf("\t\tWelcome to Binary search\n");
printf("Enter the length of the Array: ");
scanf("%d",&n);
printf("Please Enter sorted list\n");
printf("Enter the elements\n");
for (i = 0; i < n; i++)
{
printf("[%d]: ",i);
scanf("%d",&a[i]);
}
beg = 0;
end = n-1;
mid = (beg+end)/2;
printf("Enter the element u wanna find: ");
scanf("%d",&find);
while(beg<end){
if(a[mid]<find){
beg = mid+1;
}
else if(a[mid]==find){
printf("%d found at %d index",find,mid);
label = 1;
break;
}
else{
end = mid-1;
}
mid = (beg+end)/2;
}
if(label==0){
printf("Sorry Element not found\n");
}
}
Comments
Post a Comment