// Created on iPad spades.
#include <iostream>
using namespace std;
template < class Type >
int BinarySearch(Type a[], 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返回其索引
if(x > a[middle]) left = middle+1; // 转向middle的右半部分
else right = middle-1; // 转向middle的左半部分
}
return -1; // 未找到x
}
int main() {
int a[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
int x = 6;
cout<<BinarySearch(a, x, 9);
return 0;
}