-2

Here is the code am trying to execute., Obvious on the code returning a -negative value, But any reason why is it returning -7?? I vaguely understand it is based on the length of the arrary, but can someone explain more., Thanks for your help.

    public class FindNumberFromBinary {
    String myBubbleStr[] = {"Zoon","Hello","World", "Yep", "Yow", "MyData"};

    public String findString() {
        System.out.println(Arrays.binarySearch(myBubbleStr, "yow"));

        return "";
    }
    public static void main(String args[]){
        FindNumberFromBinary fnb= new FindNumberFromBinary();
        fnb.findString();
    }
}
3
  • 1
    You can only use binarySearch on sorted array. On unsorted array, result is undefined. Commented Aug 6, 2013 at 19:01
  • possible duplicate of java Arrays.binarySearch problem Commented Aug 6, 2013 at 19:13
  • Tks, You are correct, but I was confused when the compiler gave me a value of -7 for the System.out. Any tghts, greatly appreciated. Commented Aug 6, 2013 at 19:13

4 Answers 4

3

Your array must be sorted in order for a binary search algorithm to execute successfully.

Once its sorted, check the documentation.

Specifically:

Returns the index of the search key, if it is contained in the array; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

We know the element doesn't exist in this array as the value returned is negative. Also the docs say -7 is -(insertion point -1). Let's do the math and calculate where this element should be if we want to insert in sorted order.

-(-7+1) = 6

So the positive, real insertion point we'd want in this case is 6.

As your array is not sorted, this value is useless. Just some help for when this happens once your array is sorted.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I was hoping for a -6 as well. But the compiler output was -7. That confused me a bit.,
That behavior is nice once you understand it, but confusing at first. Remember to sort your array before running a binary sort. That's the biggest issue here. Also the reason the negative value is -7 and not -6 is because there would be ambiguity at index 0 without the -1 in -(insertionPoint - 1)
2

You searched for "yow" but your array has "Yow" (captial Y). The return value is the negative index + 1 of where that element would be if it were in the list.

Edit: Also, your array needs to be sorted.

3 Comments

While it is true that the array needs to be sorted... this is also a problem.
You can just use a for loop to search for the string: for(int x = 0;x < myBubbleStr.length;x++){ if(myBubbleStr[x].equals("somestr") return true;}
Thanks, I had the search term in lower case on purpose. But I was not sure why was I getting -7, the value I was expecting was -6.
1

Your array needs to be sorted to perform binary search. Please refer this link for similar kind of issue. You can sort using something like this :

java.util.Arrays.sort(myBubbleStr);

Also Array.binarySearch does not perform case insensitive match. Yo have passed wrong argument to the binerysearch method. It should be something like this :

Arrays.binarySearch(myBubbleStr, "Yow");

because your array contains 'Yow' not 'yow'

Comments

0

It's returning -7 for a couple of reasons.

  1. The return value is negative because the string you are searching for isn't present.
  2. The absolute value of the return value is 7, because that's the index of the array your search key would occupy if it were present.

The second response might seem a bit nebulous at first, but binarySearch assumes that your array is sorted to begin with (in lexicographical order when using String), and yours is not.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.