-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_style_binary.cpp
More file actions
50 lines (42 loc) · 1.11 KB
/
Copy pathleetcode_style_binary.cpp
File metadata and controls
50 lines (42 loc) · 1.11 KB
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
#include <iostream>
#include <vector>
using namespace std ;
class bin{
public:
void binary(int target,vector<int> arr)
{
int low = 0 ;
int high = 0;
int size = sizeof(arr)/sizeof(arr[0]);
high = size - 1;
int i = 0 ;
while (low <= high)
{
int mid = (low + high )/ 2 ;
if(arr[mid] > target && arr[mid] != target)
{
high = mid - 1 ;
}
else if(arr[mid] < target && arr[mid] != target)
{
low = mid + 1;
}
else if(arr[mid] == target)
{
cout << arr[mid] << " is at index " << mid ;
break;
}
i++;
}
if(low > high)
{
cout << "out of bound" ;
}
}
};
int main ()
{
vector<int> arr = {1,2,3,4,5,6,7,8,9,10};
bin b;
b.binary(6,arr); //work till target 6
}