参考文章:https://www.zhihu.com/question/36132386
https://www.oschina.net/code/snippet_126720_6472
http://www.cnblogs.com/cchun/archive/2012/07/24/2605991.html
方法1:对于不下降序列a,n为序列a元素的个数,key为关键字:
(1)求最下的i,使得a[i]=key,若不存在,则返回-1
int binary_search_1(int a[], int n, int key)
{
int m, l = 0, r = n - 1;//闭区间[0, n - 1]
while (l < r)
{
m = l + ((r - l) >> 1);//向下取整
if (a[m] < key) l = m + 1;
else r = m;
}
if (a[r] == key) return r;
return -1;
}
作者:LightGHLi
链接:https://www.zhihu.com/question/36132386/answer/105595067
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
(2)求最大的i,使得a[i] = key,若不存在,则返回-1
int binary_search_2(int a[], int n, int key)
{
int m, l = 0, r = n - 1;//闭区间[0, n - 1]
while (l < r)
{
m = l + ((r + 1 - l) >> 1);//向上取整
if (a[m] <= key) l = m;
else r = m - 1;
}
if (a[l] == key) return l;
return -1;
}
作者:LightGHLi
链接:https://www.zhihu.com/question/36132386/answer/105595067
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
int binary_search_3(int a[], int n, int key)
{
int m, l = 0, r = n - 1;//闭区间[0, n - 1]
while (l < r)
{
m = l + ((r - l) >> 1);//向下取整
if (a[m] <= key) l = m + 1;
else r = m;
}
if (a[r] > key) return r;
return -1;
}
作者:LightGHLi
链接:https://www.zhihu.com/question/36132386/answer/105595067
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
int binary_search_4(int a[], int n, int key)
{
int m, l = 0, r = n - 1;//闭区间[0, n - 1]
while (l < r)
{
m = l + ((r + 1 - l) >> 1);//向上取整
if (a[m] < key) l = m;
else r = m - 1;
}
if (a[l] < key) return l;
return -1;
}
作者:LightGHLi
链接:https://www.zhihu.com/question/36132386/answer/105595067
来源:知乎
著作权归作者所有,转载请联系作者获得授权。
int ans = std::lower_bound(a, a + n, key) - a;
ans = (ans == n || a[ans] != key) ? -1 : ans;
(2)
int ans = std::upper_bound(a, a + n, key) - a;
ans = (ans == 0 || a[ans - 1] != key) ? -1 : ans - 1;
(3)
int ans = std::upper_bound(a, a + n, key) - a;
ans = (ans == n) ? -1 : ans;
(4)
int ans = std::lower_bound(a, a + n, key) - a;
ans = (ans == 0) ? -1 : ans - 1;
binary_search()
bool binary_search(ForwardIterator first, ForwardIterator last, const LessThanComparable& value);
bool binary_search(ForwardIterator first, ForwardIteratorlast, const T& value, StrictWeakOrdering comp);
在[first,last)中查找value,如果找到返回Ture,否则返回False
二分检索,复杂度O(log(last-first))
(4)STL中的includes可以判断一个区间是否是另一个区间的子集。注意,这里说的是子集,比如1 3 5是1 2 3 4 5的一个子集。
#include
#include
#include
#include
using namespace std;
int main()
{
vector coll;
vector sub;
for(int i = 1; i <= 9; ++i)
{
coll.push_back(i);
}
sub.push_back(2);
sub.push_back(4);
sub.push_back(6);
cout << "coll : " << endl;
copy(coll.begin(), coll.end(), ostream_iterator(cout, " "));
cout << endl;
cout << "sub : " << endl;
copy(sub.begin(), sub.end(), ostream_iterator(cout, " "));
cout << endl;
if(includes(coll.begin(), coll.end(), sub.begin(), sub.end()))
{
cout << "sub is a sub set of coll" << endl;
}
else
{
cout << "not a sub set" << endl;
}
return 0;
}
#include
#include
#include
#include
using namespace std;
int main()
{
vector v;
vector::iterator iter;
pair::iterator, vector::iterator> vecpair;
for (int i = 1; i <= 20; i++) {
v.push_back(i % 6);
}
sort(v.begin(), v.end());
cout << "array: " << endl << " ";
copy(v.begin(), v.end(), ostream_iterator(cout, " "));
cout << endl << endl;
/* lower_bound */
cout << "lower_bound function, value = 3: " << endl;
iter = lower_bound(v.begin(), v.end(), 3);
cout << " [first, iter] = ";
copy(v.begin(), iter, ostream_iterator(cout, " "));
cout << endl;
cout << " [iter, last] = ";
copy(iter, v.end(), ostream_iterator(cout, " "));
cout << endl << endl;
/* upper_bound */
cout << "upper_bound function, value = 3: " << endl;
iter = upper_bound(v.begin(), v.end(), 3);
cout << " [first, iter] = ";
copy(v.begin(), iter, ostream_iterator(cout, " "));
cout << endl;
cout << " [iter, last] = ";
copy(iter, v.end(), ostream_iterator(cout, " "));
cout << endl << endl;
/* equal_range */
cout << "euqual_range function value = 3: " << endl;
vecpair = equal_range(v.begin(), v.end(), 3);
cout << " [vecpair->first, vecpair->second] = ";
copy(vecpair.first, vecpair.second, ostream_iterator(cout, " "));
cout << endl << endl;
/* binary_search */
cout << "binary_search function value = 3: " << endl;
cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "" : "not ") << " in array" << endl;
cout << endl;
/* binary_search */
cout << "binary_search function value = 6: " << endl;
cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "" : "not ") << " in array" << endl;
cout << endl;
}