//二分查找
#include<iostream>
using namespace std;
typedef char DataType;
int mySearch(DataType *ts, int n, const DataType d){
int L = 0;
int R = n - 1;
while(L<=R){
int M = (L + R)/2;
if(ts[M] == d){
return M;
}
if(ts[M] < d){
L = M+1;
}else{
R = M-1;
}
}
return -1;
}
void main(){
char cs[6] = {'*','a','b','c','d','e'};
cout<<mySearch(cs,6,'*')<<endl;
cout<<mySearch(cs,6,'a')<<endl;
cout<<mySearch(cs,6,'b')<<endl;
cout<<mySearch(cs,6,'e')<<endl;
}
二分查找
最新推荐文章于 2020-06-13 16:28:20 发布