LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-27-2014, 08:02 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
return value problem in recursive binary search


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;
}
 
Old 08-27-2014, 08:23 AM   #2
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
When you call binary_search recursively you are ignoring it's return value:
Code:
bool binary_search(int *haystack, int value, unsigned lo, unsigned hi) {
    unsigned mid;
    if (lo > hi) {
         return false;
    }
    mid = (hi + lo) / 2;
    if (haystack[mid] == value) {
        return true;
    } else if (haystack[mid] > value) {
        return binary_search(haystack,value, lo, mid - 1);
    } else if (haystack[mid] < value) {
        return binary_search(haystack,value, mid + 1, hi);
    }
}
 
1 members found this post helpful.
Old 08-27-2014, 08:30 AM   #3
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Is there any way to use a recursive call with a return value, as in my original code?
 
Old 08-27-2014, 08:53 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by atlantis43 View Post
Is there any way to use a recursive call with a return value, as in my original code?
You put a return in front of the recursive call... like in mina86's post. Or if you mean something else, can you elaborate?
 
Old 08-27-2014, 08:57 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
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
}
 
Old 08-27-2014, 09:17 AM   #6
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-27-2014, 09:19 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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);
}

Last edited by NevemTeve; 08-27-2014 at 09:22 AM.
 
Old 08-27-2014, 10:13 AM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
Quote:
Originally Posted by atlantis43 View Post
Well, here's the long (and confused) explanation for my question:
Yeah, still confused. Doesn't mina86's post #2 answer your question?

Quote:
I,ve tried to use it to subsequently call a recursive function, by means of the following:
Doesn't that work?

Quote:
Then I'd like an int returned from my originally posted code.
You can rename bool -> int, true -> 1, false -> 0. It doesn't really make any difference in C.

Quote:
I know I can call a recursive function directly, but cant pass the "lo" argument as per the instructions.
There's no "lo" argument in the search function, but binary_search is a different function.
 
1 members found this post helpful.
Old 09-15-2014, 04:16 PM   #9
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
OMG: Mina's post finally sank into my thick head!!!!!
 
Old 09-15-2014, 05:50 PM   #10
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by atlantis43 View Post
Given the following function to write, (and not allowed to change this call)
Code:
bool search(int value, int values[], int n)
Do you have to be write a recursive function? A loop would be more efficient:
Code:
bool search(int value, const int *values, int n) {
   int lo, mid, hi;

   lo = 0;
   hi = n - 1;

   while (true) {
      if (lo > hi) {
         return false;
      } else {
         mid = (hi + lo) / 2;
         if (values[mid] == value) {
            return true;
         } else if (values[mid] > value) {
            hi = mid - 1;
         } else {
            lo = mid + 1;
         }
      }
   }
}
Note that you don't need a separate binary_search function.

Last edited by psionl0; 09-17-2014 at 04:09 PM.
 
1 members found this post helpful.
Old 09-16-2014, 03:20 PM   #11
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
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).
 
Old 09-16-2014, 09:04 PM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
FYI, gcc recognizes the tail recursion and generates looping code.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: Return from function problem in search script SilversleevesX Programming 4 09-23-2011 04:41 PM
DJBDNS doesnt return AUTHORITY & ADDITIONAL SECTIONS during recursive queries debloxie Linux - Server 0 04-22-2010 04:13 AM
Recursive directory search janel10 Linux - Newbie 2 08-26-2008 06:28 AM
How to do non-recursive Binary Tree LNR Traversing? Mr_Shameless Programming 3 04-29-2007 09:35 PM
using return in recursive function hubabuba Programming 9 10-10-2005 09:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration