二分查找
程序员文章站
2022-03-02 22:47:31
...
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int l_binary_search(vector<int> v,int begin,int end,int value)
{
cout << "搜索区间" << begin << ":" << end << endl;
int mid = (begin+end)/2;
if(v[mid]==value)
return mid;
if(v[mid]>value)
return l_binary_search(v,begin,mid-1,value);
if(v[mid]<value)
return l_binary_search(v,mid+1,end,value);
}
int main()
{
int temp;
vector<int> v;
for(int i = 0;i < 1000;i++)
{
//cin >> temp;
v.push_back(i);
}
//cout << "ok" << endl;
cout << l_binary_search(v,0,v.size()-1,500);
}