//二分查找
#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;
}
二分查找
最新推荐文章于 2025-03-26 22:32:54 发布
