1 Star 0 Fork 1

codes_test/cpp-interview

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
SequentialSearch.h 548 Bytes
一键复制 编辑 原始数据 按行查看 历史
// 顺序查找
int SequentialSearch(vector<int>& v, int k) {
for (int i = 0; i < v.size(); ++i)
if (v[i] == k)
return i;
return -1;
}
/* The following is a Sentinel Search Algorithm which only performs
just one test in each loop iteration thereby reducing time complexity */
int BetterSequentialSearch(vector<int>& v, int k) {
int last = v[v.size()-1];
v[v.size()-1] = k;
int i = 0;
while (v[i]!= k)
i++;
v[v.size()-1] = last;
if(i < v.size()-1 || v[v.size()-1] == k)
return i;
return -1;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codes_test/cpp-interview.git
git@gitee.com:codes_test/cpp-interview.git
codes_test
cpp-interview
cpp-interview
master

搜索帮助