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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:58:55+00:00 2026-06-15T13:58:55+00:00

I was studying recursion problem I was going to call it, but it shows

  • 0

I was studying recursion problem
I was going to call it, but it shows like this

import java.util.Arrays;


public class BinarySearch {
public static int binarySearch(int [] list, int key){
    int low = 0;
    int high = list.length -1;
    return binarySearch(list, key, low ,high);
}
public static int binarySearch(int [] list, int key, int low, int high){
    if(low > high){
        return (-low -1);}
    int mid = (low + high) / 2;
    if(key < list[mid])
        return binarySearch(list, key, low, mid - 1);
    else if(key == list[mid])
        return mid;
    else
        return binarySearch(list, key, mid + 1, high);
}
public static void main (String [] args){
    int [] list = {'1', '2','4','5'};
    binarySearch(list, 4);
    System.out.println(Arrays.toString(list));

}

}

output : [49, 50, 52, 53]

what should I do to make it correct?

  • 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-15T13:58:56+00:00Added an answer on June 15, 2026 at 1:58 pm

    You are storing characters in integer array: –

    int [] list = {'1', '2','4','5'};
    

    So, the values you obtained is the ASCII code for 1, 2, 4, 5 respectively. Due to this, your binarySearch method would not be able to find the value 4 ever. As, it is not exactly there. Remove those single quotes around your values.

    Secondly, you are not printing the return value of binarySearch method: –

    binarySearch(list, 4);
    

    should be: –

    System.out.println(binarySearch(list, 4));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Studying Java, I've come across generic methods . public <T> void foo(T variable) {
I studying NSCharacterset class. NSString and NSCharacterset look like similar class. Who can explain
when studying RMI sometimes (head first Java) dudes use Naming.rebind(name, object) but other peoples
After studying the Perl require docs and other links like this on Stackoverflow I'm
I'm studying java web services and trying to follow the oracle tutorial but it
I'm studying for an exam, and this is a problem from an old test:
Sorry if this is obvious, but I'm studying c++ and Cuda right now and
I am currently studying recursion in school, and I have trouble thinking about methods
In studying actionscript 3's graphics class, I've come across the undocumented drawRoundRectComplex() method. It's
I'm studying this book (Addison Wesley Windows System Programming 4th Edition) and I think

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.