#include<stdio.h>
void accept_input(int a[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("[%d]=",i);
scanf("%d",&a[i]);
}
}
void display(int a[], int n)
{
int i;
for(i=0;i<n;i++)
printf("[%d]=%d\n",i,a[i]);
}
int binsrch_nonrec(int a[], int n, int x)
{
int low,high,mid;
low = 0;
high = n-1;
while(low<=high)
{
mid = (low+high)/2;
if(a[mid]==x) return mid;
else if(x<a[mid]) high=mid-1;
else low=mid+1;
}
return -1;
}
void main()
{
int nos[100],n,x,pos,ch;
clrscr();
printf("Enter no.of elements:");
scanf("%d",&n);
accept_input(nos,n);
do
{
printf("Enter element to be searched:");
scanf("%d",&x);
display(nos,n);
pos = binsrch_nonrec(nos,n,x);
if(pos==-1)
printf("Element %d not found\n",x);
else
printf("Element %d found at position %d\n",x,pos);
printf("Continue Y(1)/N(0)?");
scanf("%d",&ch);
}while(ch==1);
}
Output:
Enter no.of elements: 4
[0]=11
[1]=12
[2]=13
[3]=14
Enter element to be searched:13
[0]=11
[1]=12
[2]=13
[3]=14
Element 13 found at position 2
Continue Y(1)/N(0)?0
/
Recent Posts
Posted on 2019-07-18
Posted on 2019-07-18
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-07-17
Posted on 2019-05-28
Posted on 2019-05-24
Posted on 2019-05-24
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23
Posted on 2019-05-23