代码如下:

 1 template <class T> int binarySearch(const T* pt, int n, T t)
 2 
 3 {
 4  int head = 0, tail = n-1;
 5  int i;
 6  while (tail >= head)
 7  {
 8   i = (head + tail) / 2;
 9   if (t == pt[i])
10    return i;
11   if (t < pt[i])
12    tail = i - 1;
13   else
14    head = i + 1;
15  }
16  return -1;
17 }

参考:

http://chenkegarfield.blog.163.com/blog/static/62330008200911191540807/

posted on 2014-07-28 18:42  mobileliker  阅读(169)  评论(0编辑  收藏  举报