how to write binary search program in c using recurssion
+2
A:
For C:
Try bsearch()
from <stdlib.h>
For C++:
Try std::binary_search()
: http://www.cplusplus.com/reference/algorithm/binary_search/
PeterK
2010-08-03 06:39:12
That's C++ and not C. For C I'd suggest bsearch() in `<stdlib.h>`
AProgrammer
2010-08-03 06:43:24
I see, sorry overlooked the tags. Edited my answer accordingly.
PeterK
2010-08-03 07:02:26
A:
I found this with a quick search in Google:
http://www.dreamincode.net/forums/topic/47888-recursive-binary-search/
Hope it helps!
Karasutengu
2010-08-03 06:39:19
A:
#include<stdio.h>
int main(){
int a[10],i,n,m,c,l,u;
printf("Enter the size of an array->");
scanf("%d",&n);
printf("\nEnter the elements of the array->");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\nThe elements of an array are->");
for(i=0;i<n;i++){
printf(" %d",a[i]);
}
printf("\nEnter the number to be search->");
scanf("%d",&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf("\nThe number is not in the list");
else
printf("\nThe number is found");
return 0;
}
int binary(int a[],int n,int m,int l,int u){
int mid,c=0;
if(l<=u){
mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}