unsuccessfully searches return int index that represent the insertion point. The insertion point is the place in the collection where the element would be inserted to keep the Collection properly sorted.Because positive return value & 0 indicate successfully searches. binarySearch() uses negative number to indicate insertion point is -1.Therefore, the actual insertion point is (-(insertion point)-1).If the insertion point search at element 2, the actual insertion point return will be -3.
My question is unsuccessfully binarySearch() what it return. index of the element or insertion point. what?
Originally posted by Ami Ambre: unsuccessfully searches return int index that represent the insertion point. The insertion point is the place in the collection where the element would be inserted to keep the Collection properly sorted.
Your answer !
Originally posted by Ami Ambre:
My question is unsuccessfully binarySearch() what it return. index of the element or insertion point. what?
Your question !
Is that you want to clarify or I`m not getting it ? [ September 17, 2008: Message edited by: Sagar Rohankar ]
binarySearch() method returns the index of the search key if it is present in the list. otherwise, it returns (-(insertion point) - 1)
For eg: Your list/array is 1 3 5 8 9.
If you search for the element 5 it returns 2 which is the index of the key. If you search for the element 4 it returns a negative number -3 (-2-1=-3). Because insertion index/point for the element 4 would be 2. So it returns -3.