Hello World!

二分查找算法

static int BinarySearch(int[] arr, int dest)
{
int rs = -1;
int low = 0;
int hight = arr.Length - 1;
while (low <= hight)
{
int mid = (low + hight) / 2;
if (dest == arr[mid])
{
rs = arr[mid];
break;
}
else if (dest < arr[mid])
{
hight = mid - 1;
}
else if (dest > arr[mid])
{
low = mid + 1;
}
}
return rs;
}

posted @ 2012-05-08 22:41  世界万物  阅读(90)  评论(0编辑  收藏  举报
Bye World!