http://ideone.com/ptP0p4#include#define N 6int BinarySearch(int input[N], int beg, int end ){ int mid = (beg+end)/2; if (input[mid] == 0) return BinarySearch(input, mid+1, end); else { if(input[mid-1] == 0) return mid; else return BinarySearch(input, beg, mid-1); }}int main(){int a[N]={0,0,0,1,1,1};printf("1 starts at index %d", BinarySearch(a,0,N-1));}
http://ideone.com/ptP0p4
ReplyDelete#include
#define N 6
int BinarySearch(int input[N], int beg, int end )
{
int mid = (beg+end)/2;
if (input[mid] == 0)
return BinarySearch(input, mid+1, end);
else
{
if(input[mid-1] == 0)
return mid;
else
return BinarySearch(input, beg, mid-1);
}
}
int main()
{
int a[N]={0,0,0,1,1,1};
printf("1 starts at index %d", BinarySearch(a,0,N-1));
}