int findElement(int findItem, vector<int> searchList)
{
int listSize = searchList.size();
// if there are no elements, return NOT_FOUND
if (listSize == 0)
return NOT_FOUND;
// if there is only one element, if that's it, return 0
// if not, it's not there, so return NOT_FOUND
if (listSize == 1)
if (searchList.at(0) == findItem)
return 0;
elsereturn NOT_FOUND;
// find the middle element
int middlePosition = listSize / 2;
int middleItem = searchList[middlePosition];
// compare element at middlePosition to findItem
if (middleItem == findItem) {
return middlePosition;
}
elseif (middleItem > findItem) {
vector<int> newListBot = copyBottomHalfList(middlePosition, searchList);
findElement(findItem, newListBot);
}
else {
vector<int> newListTop = copyTopHalfList(middlePosition, searchList);
findElement(findItem, newListTop);
}
return 0;
}
Since you are recursively dividing the list in half, eventually you end up with a very small list that has such return vector positions?
There's no need to do that. Merely move the index, start and endpoint appropriately, but use the full (and original) vector. Copying vectors like this is likely slow, though I can't see how that's done in the two copy functions used here.
you make recursive calls, but don't use their result
also, the index is relative to the subvector, so you would need to add an offset when you check the top half.