-
Notifications
You must be signed in to change notification settings - Fork 28
/
BinarySearch.cpp
44 lines (42 loc) · 986 Bytes
/
BinarySearch.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
//This program demonstates how to create a binary search function in c++.
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int binarySearch(vector<int> v, int To_Find)
{
int lo = 0, hi = v.size() - 1;
int mid;
// This below check covers all cases , so need to check
// for mid=lo-(hi-lo)/2
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
if (v[mid] < To_Find) {
lo = mid + 1;
}
else {
hi = mid;
}
}
if (v[lo] == To_Find) {
cout << "Found"
<< " At Index " << lo << endl;
}
else if (v[hi] == To_Find) {
cout << "Found"
<< " At Index " << hi << endl;
}
else {
cout << "Not Found" << endl;
}
}
int main()
{
vector<int> v = { 1, 3, 4, 5, 6 };
int To_Find = 1;
binarySearch(v, To_Find);
To_Find = 6;
binarySearch(v, To_Find);
To_Find = 10;
binarySearch(v, To_Find);
return 0;
}