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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:30:21+00:00 2026-05-26T05:30:21+00:00

Hey there I’m using a binary search method to find a number in an

  • 0

Hey there I’m using a binary search method to find a number in an array, and I’m getting a StackOverflow error when looking for a number other than the one in the middle.

Here is my code:

public static  <T extends Comparable< ? super T>>
    int find(T [] a, T x, int low, int high){
    if(low>high)
        throw new IllegalArgumentException();
    int tmp = (high-low)/2;

    if(a[tmp].compareTo(x)==0)
        return tmp;
    else if(a[tmp].compareTo(x)>0)
        find(a,x,tmp,high);
    else if(a[tmp].compareTo(x)<0)
        find(a,x,low,tmp);
    return -1;
}

Also, if I try to look for a number under tmp, it returns -1.
I feel like I’m missing something but can’t figure out what.
Thanks in advanced!

  • 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-26T05:30:21+00:00Added an answer on May 26, 2026 at 5:30 am

    This is the problem:

    if(a[tmp].compareTo(x)>0)
        find(a,x,tmp,high);
    else if(a[tmp].compareTo(x)<0)
        find(a,x,low,tmp);
    

    You should be using tmp + 1 in the first case and tmp - 1 in the second. Otherwise if you’ve got (say) low = 0, high = 1 then you’ll potentially end up perpetually calling with the same arguments; tmp will end up being 0, and if x is more than a[0] you’ll just call find(a, x, 0, 1) again.

    That’s my gut feeling, anyway. You should really log what happens in terms of the low/high values being used – I’m sure you’ll see some sort of repetition before it croaks.

    EDIT: You’ve also got the comparison round the wrong way. a[tmp].compareTo(x) will return a value less than 0 if a[tmp] is less than x – i.e. you ought to look later in the array, not earlier.

    EDIT: Currently your “exit with -1” code is broken – you’ll only ever return -1 if a[tmp].compareTo(x) returns a non-zero value which is neither above zero nor below zero. I challenge you to find such an integer 🙂 (It would also do it if compareTo were unstable, but that’s a separate issue…)

    One option is to detect if high == low – at that point, if you haven’t hit the right value, you can return -1:

    int comparison = a[tmp].compareTo(x); // Let's just compare once...
    if (comparison == 0) {
       return tmp;
    }
    // This was our last chance!
    if (high == low) {
       return -1;
    }
    // If a[tmp] was lower than x, look later in the array. If it was higher than
    // x, look earlier in the array.
    return comparison < 0 ? find(a, x, tmp + 1, high) : find(a, x, low, tmp - 1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey there I've just been looking at using JQuery on my website I am
Hey there. My app is going to be using an array of 64 ImageButtons
Hey there, I've got a block of HTML that I'm going to be using
Hey there, trying to install a ruby gem but I keep getting the following
Hey there, In class we're using the Scite editor with gcc and its a
Hey there stack overflow enthusiasts. I am getting this mixed content warning in Internet
Hey there, i have a table with a field called file full of binary
Hey there, So I've been heavily focused on design/development using web technology for the
hey there i'm using mysql in hebrew , but when i try to preview
Hey there. I'm using Git for windows (The msysgit project). And working with the

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.