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 6653653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:17:47+00:00 2026-05-26T01:17:47+00:00

I have a problem with the return value in a binary search that I

  • 0

I have a problem with the return value in a binary search that I wrote.

I have the following:

int binarySearch(char *instructions[], int low, int high, char *string);

int main() {
    char *instructions[];  // some array of strings - it does not include the string "jk"
    char *string = "jk";
    int high = inst_len;
    int x = binarySearch(instructions, 0, high, string);
    if (x == -1)
        printf("not found");
    else if (x == -2)
    printf("error");
    else
    printf("Found at %d", x);
}

int binarySearch(char *instructions[], int low, int high, char *string) {

    int mid = low + (high - low) / 2;

    // Not found
    if (high <= low)
        return -1;

    // If instructions[mid] is less than string
    else if (strcmp(instructions[mid], string) > 0)
        binarySearch(instructions, low, mid-1, string);

    // If instructions[mid] is larger than string
    else if (strcmp(instructions[mid], string) < 0)
        binarySearch(instructions, mid+1, high, string);

    // Return position
    else
        return mid;
}

No matter what, in main, binarySearch is always returning 0. However, when I put print statements in the binary search algorithm, I get -1 being returned. Why is this happening?? This is very weird!

  • 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-05-26T01:17:47+00:00Added an answer on May 26, 2026 at 1:17 am

    You want this:

    // If instructions[mid] is less than string
    else if (strcmp(instructions[mid], string) > 0)
        return binarySearch(instructions, low, mid-1, string);
    
    // If instructions[mid] is larger than string
    else if (strcmp(instructions[mid], string) < 0)
        return binarySearch(instructions, mid+1, high, string);
    

    Note the “return” part.

    You’re also doing the compare more times than you have to; you can store the result of strcmp so that you only do it once:

    int r = strcmp(instructions[mid], string);
    
    if (r > 0)
        return binarySearch(instructions, low, mid-1, string);
    else if (r < 0)
        return binarySearch(instructions, mid+1, high, string);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with return statment >.< I want to store all magazine names
I have a problem, I am using the method in listview ListView.SelectedItems[0] to return
I have the following problem. The alert allways returns undefined, but I know it
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
Im having a very strange problem, i have a complicated view that returns incorrect
I have problem in some JavaScript that I am writing where the Switch statement
Updated I have the following custom field class: class Binary(models.Field): __metaclass__ = models.SubfieldBase def
I have a problem with a recursive SQL function. The original problem is that
Hey, I have been asked to write a recursive binary search for my data
I have a function that returns an 8 digit long binary string for given

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.