# Searching Algorithm

//The following idiom, usable with both forms of the binarySearch operation,
//looks for the specified search key and inserts it at the appropriate position
// if it's not already present.

int pos = Collections.binarySearch(list, key);
if (pos < 0)
l.add(-pos-1, key);

The binarySearch algorithm searches for a specified element in a sorted List.

This algorithm has two forms. The first takes a List and an element to search for (the "search key"). This form assumes that the List is sorted in ascending order according to the natural ordering of its elements. The second form takes a Comparator in addition to the List and the search key, and assumes that the List is sorted into ascending order according to the specified Comparator. .

## Searching Algorithm — Structure map

Clickable & Draggable!