//数组a[]中有n个元素,已经按升序排序,待查找的元素X template<class Type> { int BinarySearch(Type a[],const Type& x,int n) { int left = 0; //左边界 int right = n-1; //右边界 while(left <= right) { int middle = (left+right)/2; //中点 if (x == a[middle]) return middle; //找到x,返回数组中的位置 else if (x > a[middle]) left = midlde + 1; else right = middle - 1; } return -1; //未找到x } }