-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgos.cpp
More file actions
54 lines (45 loc) · 779 Bytes
/
Copy pathalgos.cpp
File metadata and controls
54 lines (45 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <vector>
bool linear_search(const std::vector<int>& vect, const int key) noexcept
{
for( const auto& entry : vect )
{
if( entry == key )
{
return true;
}
}
return false;
}
bool binary_search(const std::vector<int>& vect, const int key)
{
auto low = 0;
auto high = static_cast<int>( vect.size() ) - 1;
auto midpoint = [] (int a, int b) { return ( (a + b) / 2); };
while( low <= high )
{
const auto mid = midpoint(low, high);
if( vect[mid] < key )
{
low = mid + 1;
}
else if( vect[mid] > key )
{
high = mid - 1;
}
else
{
return true;
}
}
return false;
}
std::vector<int> generate_vector( const int n )
{
std::vector<int> vect;
vect.resize(n);
for( int i = 0; i < n; ++i )
{
vect[i] = i;
}
return vect;
}