You are on page 1of 1

#include <stdio.

h>
int bsearch(int find,int first_index,int last_position,int array[]){
int mid;
if(last_position-first_index==0)
return -1;
mid = (first_index+last_position-1)/2;
if(find==array[mid])
return mid;
if(find>array[mid])
return bsearch(find,mid+1,last_position,array);
else
return bsearch(find,first_index,mid,array);
}
int main(int argc, char **argv)
{
int a[]={1,2,3,4,5,7,9,12,23,34,45,56,67,78,89,90};
int pos = bsearch(2,0,sizeof(a)/sizeof(*a),a);
if(pos>=0)
printf("position of element = %d",pos+1);
else printf("not found");
return 0;
}

You might also like