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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:47:33+00:00 2026-06-16T11:47:33+00:00

Possible Duplicate: Arrays.binarySearch doesnt work like it should From my understanding of BinarySearch, it

  • 0

Possible Duplicate:
Arrays.binarySearch doesnt work like it should

From my understanding of BinarySearch, it would only return an insertion point IF the element is not found in the array or collection.

So this code has me a little confused:

import java.util.*;

public class LookinGlass {

    public static void main(String args[]){

        String[] asa = {"x", "c", "b", "d"};

        int x  = Arrays.binarySearch(asa, "b");

        System.out.println("Index is at " + x);

        Arrays.sort(asa);

        int y = Arrays.binarySearch(asa, "b");

        System.out.println(x + " " + y);
    }

}

When I print out x, why does it say that the index is at -1, when clearly “b” exists in the array and can be found?

Thanks

  • 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-16T11:47:34+00:00Added an answer on June 16, 2026 at 11:47 am

    The Binary Search algorithm assumes that it is looking at data sorted in ascending order, in your first call the array:

     String[] asa = {"x", "c", "b", "d"};
    

    is not sorted. Because of this the Arrays.binarySearch(asa, "b"); call is returning -1 because it could not find "b" in your array using the Binary Search algorithm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible duplicate : comparing-two-arrays I have two NSArray and I'd like to create a
Possible Duplicate: C# arrays , Getting a sub-array from an existing array Basically I
Possible Duplicate: Ruby: Sorting 2 arrays using values from one of them I have
Possible Duplicate: Java: Checking equality of arrays (order doesnt matter) I have two arrays
Possible Duplicate: std::vector is so much slower than plain arrays? Looks like vector is
Possible Duplicate: How do I work with dynamic multi-dimensional arrays in C? pointer to
Possible Duplicate: Why does cout print char arrays differently from other arrays? If I
Possible Duplicate: send arrays of data from php to javascript I'm sending an Array
Possible Duplicate: Comparing Two Arrays Using Perl I am trying to find elements that
Possible Duplicate: php loop through associative arrays i have a variable which prints an

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.