Shows how to do a binary search on an array, can be changed to work on any presorted list structure
static int BinarySearch(int[] data, int val)
{
int lo = 0;
int hi = data.Length - 1;
while (hi >= lo)
{
var cur = lo + ((hi - lo) >> 1);
var el = data[cur];
if (el == val)
return cur;
if (el < val)
lo = cur + 1;
else
hi = cur - 1;
}
return -1;
}