I am trying to implement recursive binary search in C++. However my algorithm cannot find the last two elements from the test array.I know that I am missing something. I have searched a lot for such a implementation of binary search algorithm but without success. Can anyone help me with it?
bool isMember (int x, int a[], int size){
if (size == 0) return false;
return a[size/2] == x ||
(a[size/2] < x && isMember (x,a+size/2,(size)/2)) ||
(a[size/2] > x && isMember (x,a,(size)/2));
}
memberfunction do?sizeis odd – for instance, when it is 3, you will divide intoa + 1with size 1, andawith size 1.a[2]has gone missing.