未验证 提交 3491cb2c 编写于 作者: 辉哈's avatar 辉哈 提交者: GitHub

Merge pull request #68 from gurucharan2206/GurucharancppLocal

BetterSequentialSearch function added to SequentialSearch.h
......@@ -4,4 +4,20 @@ int SequentialSearch(vector<int>& v, int k) {
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;
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册