C++ vector实现二分查找法

来源:互联网 发布:淘宝等级权限 编辑:程序博客网 时间:2024/05/09 23:26
#include<iostream>
#include<vector>


using namespace std;
using std::vector;
int main()
{
vector<int> text{54,87,575,4587,54245,54255,58655,59654,60000,60555,65588};
auto beg = text.begin();
auto end = text.end();
auto mid = text.begin() + (end - beg) / 2;
int sought = 60555;
while (mid != end && *mid != sought)
{
if (sought < *mid)
{
end = mid;
for (auto it = beg; it != mid; it++)
cout << *it << "  ";
}
else
{
beg = mid + 1;
for (auto it = beg; it != end; it++)
cout << *it << "  ";
}
mid = beg + (end - beg) / 2;
cout << endl;
}
cout << "position:" <<&mid<< endl;
cout << "search element:"<<*mid << endl;



cin.get();
return 0;
}
0 0
原创粉丝点击