mirror of
https://github.com/huihut/interview.git
synced 2024-03-22 13:10:48 +08:00
Merge pull request #68 from gurucharan2206/GurucharancppLocal
BetterSequentialSearch function added to SequentialSearch.h
This commit is contained in:
commit
3491cb2cf1
|
@ -5,3 +5,19 @@ int SequentialSearch(vector<int>& v, int k) {
|
||||||
return i;
|
return i;
|
||||||
return -1;
|
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…
Reference in New Issue
Block a user