posted 10 years ago

Hi,

Since the given string array has 5 elements, the method will return o-4 for successful search which are the possible indexes into the array and for uncessecufful search it would return the insertion point and for an an array with 5 elements the possible insertion points it can return are -1,-2,-3,-4,-5 and hence the method can return 0,1,2,3,4, -1,-2,-3,-4,-5 and hence the answer should be from -5 to 4.

For example the given string array has String [12,13,14,15,16] so a invocation Arrays.binarySearch(str,12) will result 0 because 12 is present @ index 0 and in a similar way it can return 0 - 4 for all successful search. If the element to be searched is present in the array then the method would return the index of the element in the array or else it will return the insertion point. Since positive return values implies successful search a -ve value to indicate insertion point and when these values inverted and decremented would indicate the index at which the given element can be inserted into the array to maintain the sorting order. So if I would invoke Arrays.binarySearch(str, 10) the return value would be -1 which implies the element 10 can be inserted @ index 0 [ index = (-(insertion point) - 1)). Since this array has 5 elements possible insertion points can be -1,-2,-3,-4,-5

I dont understand why the answer G -6 to 4 is correct in the book.

Thanks

Deepak

Since the given string array has 5 elements, the method will return o-4 for successful search which are the possible indexes into the array and for uncessecufful search it would return the insertion point and for an an array with 5 elements the possible insertion points it can return are -1,-2,-3,-4,-5 and hence the method can return 0,1,2,3,4, -1,-2,-3,-4,-5 and hence the answer should be from -5 to 4.

For example the given string array has String [12,13,14,15,16] so a invocation Arrays.binarySearch(str,12) will result 0 because 12 is present @ index 0 and in a similar way it can return 0 - 4 for all successful search. If the element to be searched is present in the array then the method would return the index of the element in the array or else it will return the insertion point. Since positive return values implies successful search a -ve value to indicate insertion point and when these values inverted and decremented would indicate the index at which the given element can be inserted into the array to maintain the sorting order. So if I would invoke Arrays.binarySearch(str, 10) the return value would be -1 which implies the element 10 can be inserted @ index 0 [ index = (-(insertion point) - 1)). Since this array has 5 elements possible insertion points can be -1,-2,-3,-4,-5

I dont understand why the answer G -6 to 4 is correct in the book.

Thanks

Deepak

SCJP, SCWCD, SCBCD

anita dhar

Ranch Hand

Posts: 69

posted 10 years ago

You have forgotten that one possible insertion point isOriginally posted by Deepak Jain:

and for an an array with 5 elements the possible insertion points it can return are -1,-2,-3,-4,-5 and hence the method can return 0,1,2,3,4, -1,-2,-3,-4,-5 and hence the answer should be from -5 to 4.

**after the last element**. So the possible insertion points are 0, 1 ,2 ,3 ,4 and 5, which are translated to -1, -2, -3, -4, -5 and -6. Conclusion, the answer in the book is correct.

anita dhar

Ranch Hand

Posts: 69

Manfred Klug

Ranch Hand

Posts: 377

Deepak Jain

Ranch Hand

Posts: 637

posted 10 years ago

If the array has 5 elements the only possible indexes are 0,1,2,3 and 4. How can we have index 5? Aren't indexes support to start from 0 and in Java if the size of the array is 5 then the indexes can be from 0-4 only.

Could you please explain how can we have index number 5?

If we can have 5 then offcourse there is another possible insertion point

-6.

Thanks

Deepak

Could you please explain how can we have index number 5?

If we can have 5 then offcourse there is another possible insertion point

-6.

Thanks

Deepak

SCJP, SCWCD, SCBCD

Manfred Klug

Ranch Hand

Posts: 377

Deepak Jain

Ranch Hand

Posts: 637

posted 10 years ago

Hi Manfred,

Thanks for making us understand how the binarySearch can return -6 in the result.

I have one doubt regarding the same question:

As we discussed

-6 : Returned when Integer is greater than current MAX(Array Element) is being searched.

-4 : Returned when Integer is less than current MAX(Array Element)is being searched.

My Condition:

Integer[] str = {1,12,9,12,10};

Arrays.sort(str);

System.out.println(Arrays.binarySearch(str,11)); //Return -4

System.out.println(Arrays.binarySearch(str,13)); //Return -6

When can we receive -5 in the search?

Thanks for making us understand how the binarySearch can return -6 in the result.

I have one doubt regarding the same question:

As we discussed

-6 : Returned when Integer is greater than current MAX(Array Element) is being searched.

-4 : Returned when Integer is less than current MAX(Array Element)is being searched.

My Condition:

Integer[] str = {1,12,9,12,10};

Arrays.sort(str);

System.out.println(Arrays.binarySearch(str,11)); //Return -4

System.out.println(Arrays.binarySearch(str,13)); //Return -6

When can we receive -5 in the search?

posted 10 years ago

Hi Dude,

The sorted array will appear something like thing

Data 1 9 10 12 12

Pos index 0 1 2 3 4

Search Index -1 -2 -3 -4 -5

You are searching for element 13 and you will get -6 always.In this scenario you can never get -5.

Change the data to get -5.

1 8 17 20 38 [Unique elements]

The sorted array will appear something like thing

Data 1 9 10 12 12

Pos index 0 1 2 3 4

Search Index -1 -2 -3 -4 -5

You are searching for element 13 and you will get -6 always.In this scenario you can never get -5.

Change the data to get -5.

1 8 17 20 38 [Unique elements]

It is sorta covered in the JavaRanch Style Guide. |