[SOLVED] return value problem in recursive binary search
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
trying to use the following code, but can't figure out how to parse it properly so that the function exits after value return true;, rather than also subsequent execution of "return false". Probably a very simple error, and hope someone can help.
Code:
int binary_search(int haystack[],int value, int lo,int hi)
{
int mid;
//int lo = 0,hi = MAX;
if (lo > hi) //would print "not found""
{
return 0;
}
mid=(hi+lo)/2;
if (haystack[mid] == value)
{
return true;
}
else if (haystack[mid] > value)
{
binary_search(haystack,value, lo, mid - 1);
}
else if (haystack[mid] < value)
{
binary_search(haystack,value, mid + 1, hi);
}
return false;
}
I do not really understand what do you need, but probably:
Code:
int binary_search(int haystack[],int value, int lo,int hi)
{
int mid;
int b;
if (lo > hi) //would print "not found""
{
return 0;
}
mid=(hi+lo)/2;
if (haystack[mid] == value)
{
return true; // << you should return int, not boolean
}
else if (haystack[mid] > value)
{
b = binary_search(haystack,value, lo, mid - 1);
}
else if (haystack[mid] < value)
{
b = binary_search(haystack,value, mid + 1, hi);
}
// calculate return value (based on b)
return something
}
Just trying to learn what I'm not understanding in the following Rube Goldberg creation!
Well, here's the long (and confused) explanation for my question:
Given the following function to write, (and not allowed to change this call)
Code:
bool search(int value, int values[], int n)
I,ve tried to use it to subsequently call a recursive function, by means of the following:
Code:
{
int needle = value;
int lo = 0,hi = n;ret;
ret=binary_search(values,needle, lo, hi);
if(ret==1)
{
return true;
}
else
return false;
}
Then I'd like an int returned from my originally posted code.
I know I can call a recursive function directly, but cant pass the "lo" argument as per the instructions.
You should return an int, not a bool. Also, if you want recursion (bad idea), you might want to use a helper function:
Code:
static int binary_search_core (const int haystack[], int needle, int lo, int hi)
{
...
}
int binary_search (const int haystack[], int n, int needle)
{
return binary_search_core (haystack, needle, 0, n-1);
}
Psion10;
Yes, I already had written a loop, but was just trying to learn recursion for same purpose (within the limitation of the given calling function).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.