Package java.util
Class Collections

Public Method binarySearch

static int binarySearch(List l, Object key)

static int binarySearch(List l, Object key, Comparator c)

Throws:

_INSERT_METHOD_SIGNATURE_HERE_

Description:

binarySearch(List l, Object key) method:

Searches the specified list for the specified key object using the binary search algorithm. The list must be sorted into ascending order according to the natural ordering of its elements (as by the sort(List) method, above) prior to making this call. If it is not sorted, the results are undefined. If the list contains multiple elements equal to the specified object, there is no guarantee which one will be found.

This method runs in log(n) time for a "random access" list (which provides near-constant-time positional access). It may run in n log(n) time if it is called on a "sequential access" list (which provides linear-time positional access).

If the specified list implements the AbstracSequentialList interface, this method will do a sequential search instead of a binary search; this offers linear performance instead of n log(n) performance if this method is called on a LinkedList object.

Returns index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

binarySearch(List l, Object key, Comparator c) method:

Like above but with additional Comparator which determines the list ordering. A null value indicates that the elements' natural ordering should be used.