Create a recursive function for the binary search. This function accepts a sorted array and a give item being search for and returns the index of the item if this give item in the array or returns -1 if this give item is not in the array. Moreover, write a test program to test your function.
Sorry for the bad english but my teacher can not write it or speak it very well. This is for a final project and determines whether I graduate or not I went to the tutor and he did not know how to do it either. Any help is greatly appreicated.
template int orderedArrayListType::binarysearch (const elemType& item) const
{
int first= 0;
int last = length -1;
int mid;
int list[];
int BinarySearch(,Type & Item, int first, int last)
bool found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid] > item)
return BinarySearch(list, item, first, mid -1)
found = true;
else if (list[mid] > item)
return BinarySearch( list, item, first, mid -1)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return -1;
}//end Binary Search