Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList BinarySearch

I'm busy preparing for the MCTS 70-536 exam, according to the exam book (Microsoft Press - .NET Framework - Application Development Foundation Self Paced Training Kit 2nd Edition), this code sample:

ArrayList al = new ArrayList();
al.AddRange(new string[] { "Hello", "world", "this", "is", "a", "test" });
Console.WriteLine(al.BinarySearch("this"));

Outputs the value '2' to the console because the item 'this' is at index 2. Agreed that is the output I get when I run that code.

However if I run

Console.WriteLine(al.BinarySearch("world"));

I would expect to get the value 1 in the console since 'world' would be at index 1, however I get the value -7 ?

Could anyone please explain how this works?

Thanks

like image 769
Kevin McKelvin Avatar asked Feb 23 '10 10:02

Kevin McKelvin


People also ask

What is arrays binarySearch?

BinarySearch(Array, Object) Searches an entire one-dimensional sorted array for a specific element, using the IComparable interface implemented by each element of the array and by the specified object.

Does binary search work on ArrayList?

BinarySearch(Object) Searches the entire sorted ArrayList for an element using the default comparer and returns the zero-based index of the element.

What is binarySearch in Java?

Binary Search in Java is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. It works only on a sorted set of elements. To use binary search on a collection, the collection must first be sorted.

How does arrays binarySearch work in Java?

Simply put, the Arrays. binarySearch() method can look for a given element in a sorted array and return its index if found. 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.


3 Answers

The array you are performing the binary search on is not sorted. This is a requirement for BinarySearch to function.

Being not sorted, confuses the search algorithm, and makes it think that "world" should be on the 7th place, but it is not in the array: the result is -7.

like image 182
GvS Avatar answered Sep 30 '22 18:09

GvS


Taken directly from MSDN documentation of ArrayList.BinarySearch (http://msdn.microsoft.com/en-us/library/5tzt7yz3%28VS.85%29.aspx)

The value parameter and each element of the ArrayList must implement the IComparable interface, which is used for comparisons. The elements of the ArrayList must already be sorted in increasing value according to the sort order defined by the IComparable implementation; otherwise, the result might be incorrect.

like image 30
wasatz Avatar answered Sep 30 '22 17:09

wasatz


Question already answered but added this for reference.

The BinarySearch will find items by using a sorted algorithm. In you example the default sort is alphabetical so "world" should be been last, hence number 7.

You will see an overloader of the BinarySearch which accepts an IComparer object. To not supply this gives the default sort of alphabetical.

But if you implemented the "reverseSort" method as the book suggests then you would need to pass the BinarySearch function the "reverseSort" object.

Like this;

Console.WriteLine(al.BinarySearch(al, new reverseSort()));

Also just as I found it quite interesting if you implement the "reverseSort" class and do a console.writeline on which values get compared, the results weren't what I'd expect. I spent a little while trying to work out what the algorithm was.

like image 43
Mike Mengell Avatar answered Sep 30 '22 17:09

Mike Mengell