#include<stdio.h>
//global declaractions
int a[5],n=5,i;
void ArrIn();
void Binary();
int main(){
ArrIn();
Binary();
return 0;
}
void ArrIn(void){
for(i=0;i<n;i++){
printf("\nEnter %d element ",i+1);
scanf("%d",&a[i]);
}
}
void Binary(){
int lb=1,ub=n,mid,find;
printf("\n Enter element which you want to find");
scanf("%d",&find);
while(lb<=ub){
mid=(lb+ub)/2;
if(find<mid){
ub=mid-1;
}
else if(find>mid){
lb=mid+1;
}
else if(find==mid){
printf("\nElement found");
break;
}
}
if(find!=mid){
printf("\nElement not found");
}
}
ALGORITHM
STEP 1:Read shorted a[n]initialize lb<-1,ub<-nSTEP 2:Repeat while lb<=ubmid<-(lb+ub)/2if(x<mid)thenub<-mid-1else if(x->mid)thenlb<-mid+1elsewrite "yes" or return midend ifend else ifend whileSTEP 3:Write a[n]Output
Java
Tech News
Ad
Binary searchC language toutorialsGoogle Searching MethodHow to Implement Binary Search
Binary search Program In c | How to implement binary search in c
Subscribe to:
Post Comments (Atom)

 
No comments:
Post a Comment