Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8719167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:43:39+00:00 2026-06-13T06:43:39+00:00

I’m 99% sure my problem is that I’m setting low to zero every start.

  • 0

I’m 99% sure my problem is that I’m setting low to zero every start. But I’m not sure how to keep low consistently representative of the low index regardless of the depth of my recursion. If it accurately told me the index of the low index I don’t think I would have a problem.

Here’s my code so far:

int recBSearch(vector<int> v, int size, int item)
{
    int index = size / 2;
    int curr = v[index];
    int low = 0;
    int high = size -1;
    if (v[index] == item)
        return index;
    else if (v[index] > item)
    {
        high = index;
        index = (high+low)/2;
        size = high - low;
        return recBSearch(v, size, item);
    }
    else if (v[index] < item)
    {
        low = index;
        index = (high+low)/2;
        size = high - low;
        return recBSearch(v, size, item);
    }
    return -1;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:43:41+00:00Added an answer on June 13, 2026 at 6:43 am

    This won’t work when you are trying to search in the upper half of the vector, because what you really need to create is a slice of the vector.

    There is already a binary search but if you are determined to write your own, use an iterator-range in the parameters. (You can either pass in two plain iterators or a boost range).

    You want -1 if not found else the iterator location, so in your slice (iterator range) you would need to specify a starting index number in case it is found.

    You could also pass, as an alternative, the vector (by const reference) and the range in which you wish to search.

    Your last line is unreachable. Instead it should be the terminating condition of your recursion before you do any evaluation. (If your range is empty)

    The version that would iterate by passing by reference and using index numbers (simplest) would look like this:

    int recBSearch( std::vector<int> const& vec, int start, int end, int value )
    {
        if( start == end )
        {
           return -1;
        }
        int index = (start + end) / 2;
        // continue from here
    }
    

    end would indicate “one past the last element” so if the vector has size 5, the first iteration would pass 0 and 5. If the vector is empty, you pass 0 and 0.

    As an exercise, “can it be done with 3 parameters”?

    Yes…

     typedef std::vector<int>::const_iterator citer;
     int recBSearch( citer start, citer end, int value )
     {
        if( start == end )
        {
           return -1;
        }
        citer middle = start + (end-start)/2;
        if( *value == *middle )
        {
           return middle - start;
        }
        else if ( *value < *middle )
        {
           return recBSearch( start, middle, value );
        }
        else // note the change here
        {
            int res = recBSearch( middle+1, end, value );
            if( res == -1 )
               return -1;
            else
               return res + 1 + (middle-start);
        }
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a small JavaScript validation script that validates inputs based on Regex. I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.