alacrite@gmail.com wrote:
Quote:
#include <iostream>
#include <vector>
>
using namespace std;
>
int binarySearch(vector<int>, int, int, int);
>
int main()
{
vector<intarrInt;
for(int i=0; i<1000; i++)
{
arrInt.push_back(i);
}
int pos = 0;
if(pos=binarySearch(arrInt, 12, 0, 999))
cout<<"pos "<<pos<<endl;
//cout<<arrInt[pos];
else
cout<<"Not Found"<<endl;
Main does not call recursive functions recursively. It can't. Only a
recursive function can call itself.
So you don't need an if condition or for that matter, any conditional
statements.
Main does not care or know that the function is recursive.
int pos = binarySearch(vn, 12, 0, vn.size() - 1);
std::cout << "pos "<< pos << std::endl;
Quote:
>
return 0;
>
}
>
>
int binarySearch(vector<intlist, int value, int left, int right)
{
int mid = (left+right)/2;
Your function creates a local copy of the vector everytime the
binarySearch function is called.
why not pass the vector by reference?
Quote:
if(value>list[mid])
binarySearch(list, value, mid, right);
Are you not attempting to call this function recursively? Don't you see
the compiler's warning about reaching the end of a function that is
expected to return an integer?
if(value list[mid]) {
return binarySearch(list, value, mid, right);
}
Quote:
else if (value<list[mid])
binarySearch(list, value, left, mid);
missing return again...
Now, if value is neither greater nor less, its obviously equal
else // equal
{
cout<<"about to return "<<mid<<endl;
return mid;
}
Quote:
else if(value==list[mid])
{
cout<<"about to return "<<mid<<endl;
return mid;
}
else
return -1;
}
>
When this runs the output I get it:
about to return 12
pos 1244568
>
expected output:
about to return 12
pos 12
>
Anyone know why the return value from binarySearch is wrong?
You'ld be surprised, the pos you see is not the pos you think. the
compiler has no choice but to create a local copy of the variable in
the if loop. And since your function was not returning, the remnants of
a local are seen in the ouput. Remember that everytime you call a
function recursively, a new stack is created (you'll see that if you
set a breakpoint in the function below). So in essense, a recursive
call which is expected to return (but does not) leaves a function stack
hanging in mid air. Thats one of the reasons these are rare in C++.
set a breakpoint in binarySearch and observe.
#include <iostream>
#include <vector>
int binarySearch(std::vector<int>& list, int value, int left, int
right)
{
int mid = (left + right)/2;
std::cout << "mid "<< mid << std::endl;
if(value list[mid]) {
return binarySearch(list, value, mid, right);
}
else if (value < list[mid]) {
return binarySearch(list, value, left, mid);
}
else { // must be equal
std::cout << "about to return " << mid << std::endl;
return mid;
}
}
int main()
{
std::vector<intvn;
for(int i=0; i<1000; i++)
{
vn.push_back(i);
}
int pos = binarySearch(vn, 12, 0, vn.size() - 1);
std::cout << "pos "<< pos << std::endl;
return 0;
}
/*
mid 499
mid 249
mid 124
mid 62
mid 31
mid 15
mid 7
mid 11
mid 13
mid 12
about to return 12
pos 12
*/