-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.cpp
57 lines (46 loc) · 1.07 KB
/
binary_search.cpp
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
55
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath> // Para floor
using namespace std;
vector<int> numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
pair<int, int> binary_search(int search, const vector<int>& list)
{
int iterations = 0;
int i = 0;
int j = list.size() - 1;
int half;
while (i <= j)
{
iterations += 1;
half = floor((i + j) / 2);
if (list[half] == search)
{
return {half, iterations};
}
else if (search > list[half])
{
i = half + 1;
}
else
{
j = half - 1;
};
};
return {-1, iterations};
};
int main()
{
pair<int, int> result = binary_search(2, numbers);
int position = result.first;
int iterations = result.second;
if (position != -1)
{
cout << "Value found in position " << position << " in " << iterations << " iterations." << endl;
}
else
{
cout << "Value hasn't been found in " << iterations << " iterations." << endl;
};
return 0;
};