代码如下:
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/
时间: 2024-10-03 13:39:00