-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_search.cpp
56 lines (45 loc) · 1.24 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
56
using namespace std;
#include <iostream>
int binary_search(int *array, int low, int high, int to_find);
int main() {
int size;
int *array;
array = new int[size];
cout <<"Write size of sequence\n";
cin >> size;
for(int i=0; i<size; i++) {
cout << "Write number: ";
cin >> array[i];
}
int to_find;
cin >> to_find;
cout << binary_search(array, 0, size-1, to_find) << endl;
return 0;
}
int binary_search(int *array, int low, int high, int to_find) {
if(low == high) {
if(array[low] == to_find) {
return low;
}
else {
return -1;
}
}
int index_found = -2;
int middle = (high + low) / 2;
if(array[middle] == to_find) {
return middle;
}
else if(array[middle] > to_find) {
index_found = binary_search(array, low, middle-1, to_find);
}
else {
index_found = binary_search(array, middle + 1, high, to_find);
}
return index_found;
}
/* The worst-case running time of binary search is O(log n) because as we split
the tree in halves, we will(at most) reach log n levels plus the first one. And
onde we reach that log n level, we will be considering only one number wich
will be the nunmber we are looking for or won't in which case there's not that
number in the ordered sequence.*/