This is what I have and it isn't finding the numebr in the list - when it is there!
Code:
template<class elemType>
int orderedArrayListType<elemType>::binarySearch(const elemType& item)
{
int first = 0;
int last = length - 1;
int mid = last - first / 2;
return binarySearchImpl(item, mid);
}
//Function written for Test 5 Question 22
template<class elemType>
int orderedArrayListType<elemType>::binarySearchImpl(const elemType& item, int i)
{
const int last = length - 1;
const int first = 0;
if(first > last || i == last || i == first)
return -1;
if(list[i] == item)
return i;
else if (list[i] > item)
{
return binarySearchImpl(item, i - 1);
}
else
{
return binarySearchImpl(item, i + 1);
}
}//end binarySearch
I had to define first and last in binarySearch().
The numbers are in the list and with this code, they aren't found.
It does compile and it doesn't give a run time error, so those are two hash marks on the C++ side!