首页 > 代码库 > 二分查找

二分查找

private int BinarySearch(List<int> list,int value)
{
  int low = 0;
  int high = list.Count - 1;
  int middle = 0;
  while(low<=high)
  {
    middle = (high-low)/2+low;
    if(list[middle]>value)
      high = middle-1;
    else if(list[middle]<value)
      low = middle+1;
    else
      return middle;
  }
  return -1;

}

  

二分查找