Featured post
Write a C program to find Binary Search Program in C program( Data Structure And Algorithm)
- Get link
- X
- Other Apps
Data Structure And Algorithm
Write a C program to find Binary Search Program in C program
#include<stdio.h>
int bserch(int a[10],int key,int lb,int ub);
int main()
{
int i,a[10],n,key,lb,ub,mid;
printf("enter the no=");
scanf("%d",&n);
printf("enter the element=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key=");
scanf("%d",&key);
mid=bserch(a,key,0,n-1);
if(key==a[mid])
{
printf("%d is found at position
%d",key,mid+1);
}
else
printf("key is not found");
}
int bserch(int a[10],int key,int lb,int ub)
{
int mid,i,n;
while(lb<=ub)
{
mid=(lb+ub)/2;
if(key<a[mid])
{
ub=mid-1;
bserch(a,key,lb,ub);
}
else
if(key>a[mid])
{
lb=mid+1;
bserch(a,key,lb,ub);
}
else
if(key==a[mid])
{
return mid;
}
}
}_____________________________________________________________________
OUTPUT
enter the no=4
enter the element=1
2
3
4
enter the key=3
3 is found at position 3amr@amr-virtual-machine:~$ gcc
1.c -o 1
amr@amr-virtual-machine:~$ ./1
enter the no=4
enter the element=1
2
3
4
enter the key=10
key is not found
_____________________________________________________________________
- Get link
- X
- Other Apps
Comments
Post a Comment
Welcome to FlutterForge please write your Query