Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.BinarySearch() return value

Tags:

c#

.net

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.

like image 413
abenci Avatar asked Jul 11 '12 15:07

abenci


People also ask

How do you do a binary search on an array?

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.

What is the difference between arrays and collections and binarysearch?

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.

What is the return type of a key in an array?

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.

How do I search an array for a specific 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.


2 Answers

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.

like image 69
GSerg Avatar answered Sep 23 '22 09:09

GSerg


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.

like image 32
James Michael Hare Avatar answered Sep 23 '22 09:09

James Michael Hare