今天上課重新講了二分查找 呵呵 回來(lái)寫個(gè)模板用用
//Get Guidance By Mr ZhangHong
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;


template<class T>
int b_search(T key,T a[],int size)


{

int mid;
int front=0;
int rear=size-1;
while(front<=rear)

{

mid=(front+rear)/2;
if(a[mid]==key)
return mid;
else if(key>a[mid])

{
front=mid+1;
continue;
}
else if(key<a[mid])

{
rear=mid-1;
continue;
}
}
if(front>rear)
return -1;
}


int main ()


{


int test[]=
{1,2,3,4,5,6,7,8,9,10,50,200};
int p;
p=b_search(13,test,sizeof(test)/sizeof(test[0]));
cout<<"該元素位于數(shù)組的第"<<p<<"號(hào)位置"<<endl;
system("pause");
return 0;

}