-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
77 lines (65 loc) · 1.9 KB
/
binarySearch.cpp
File metadata and controls
77 lines (65 loc) · 1.9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
using namespace std;
void testVector(vector<int> obj){
cout<<obj.empty();
if(!obj.empty()){
cout<<"it is not empty"<<endl;
}
while (!obj.empty())
{
cout<<obj.back()<<endl;
obj.pop_back();
}
}
bool binarySearch(int value,vector<int> obj,int _startPos, int _endPointer){
if(obj.size() == 0){
cout<<"size is zero";
return false;
}
if(_startPos == _endPointer){
if(value == obj.at(_startPos)){
return true;
}
else{
return false;
}
}
int middlePointer = (_endPointer + _startPos)/2;
if(value == obj.at(middlePointer)){
return true;
}
else if(value > obj.at(middlePointer)){
return binarySearch(value,obj,(middlePointer+1),_endPointer);
}
else {
return binarySearch(value,obj,_startPos,(middlePointer-1));
}
}
int main() {
// your code goes here
vector<int> testObj;
testObj.push_back(1);
for(int i;i<15;++i){
testObj.push_back(i+i);
}
//testVector(testObj);
std::cout << "myvector contains:";
for (unsigned i=0; i<testObj.size(); i++)
std::cout <<testObj.at(i)<<endl;
std::cout << '\n';
int input;
while(true){
cin>>input;
cout<<input<<endl;
if(input == 0)
break;
if(binarySearch(input,testObj,0,testObj.size()-1)){
cout<<"It is found"<<endl;
}
else{
cout<<"It is not found"<<endl;
}
}
return 0;
}