I have the following array:
double[] list = new double[] {0,0,100,100}
Why if I search for 29.6
I get -3
?
Array.BinarySearch(list, 29.6)
I expected +1
or -1
.
The Array.BinarySearch() documentation for the return parameter says:
The index of the specified value in the specified array, if value is found. If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value. If value is not found and value is greater than any of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
But it does not says too much to me.
Binary Search Implementation in C# language. The Array class in .NET framework supports several methods to search, sort, and reverse array items. Array.BinarySearch () method searches an an array of elements for the given element and returns the postion of the element found in the array.
Arrays.binarysearch () works for arrays which can be of primitive data type also. Collections .binarysearch () works for objects Collections like ArrayList and LinkedList. There are variants of this method in which we can also specify the range of array to search in.
Return Type: 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.
The Arrays.binarySearch () method takes the array you want to search as the first argument and the key you're looking for as the second argument. The output from this program will be: Remember, the method returns the index of the found item and not the item itself. So you can store the index in an integer like the one used in this example.
If value is not found and value is less than one or more elements in array, a negative number which is the bitwise complement of the index of the first element that is larger than value.
The first element which is larger than 29.6 is 100
, which has index of 2
.
~2
is -3
.
You can use the '~' to take the bitwise complement which will give you the index of the first item larger than the search item.
If the Array does not contain the specified value, the method returns a negative integer. You can apply the bitwise complement operator (~) to the negative result (in Visual Basic, Xor the negative result with -1) to produce an index. If this index is greater than or equal to the size of the array, there are no elements larger than value in the array. Otherwise, it is the index of the first element that is larger than value.
From the MSDN
Thus if you had:
var pos = Array.BinarySearch(list, 29.6);
You can check:
if (pos < 0)
{
Console.WriteLine("Not found, the result was {0} which is index {1}", pos, ~pos);
}
Which, in your case, means your -3
would indicate the index 2
is the first item larger than your search target.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With