-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
62 lines (59 loc) · 1.59 KB
/
binarySearch.cpp
File metadata and controls
62 lines (59 loc) · 1.59 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
#include<iostream>
#include<vector>
using namespace std;
//Time Complexity : O(logn)
//Space Complexity : O(1)
int binarySearch(vector<int> vect, int target,int n){
int start=0;
int end=n-1;
while(start<=end){
int mid = start + ((end-start)/2); //Optimization to prevent overflow condition
//int mid=(start+end)/2;
if (vect[mid]<target){
start=mid+1;
}
else if (vect[mid]>target){
end=mid-1;
}
else{
return mid;
}
}
return -1;
}
int main(){
vector<int> vect={2,3,5,6,7,8};
int target;
cout<<"Enter target: ";
cin>>target;
int n=vect.size();
int answer=binarySearch(vect,target,n);
cout<<"Target found at index : "<<answer;
}
//Recursive Binary Search
//Time Complexity: O(logn)
//Space Complexity: O(logn)
// int binarySearch(vector<int> vect, int target, int start, int end){
// int mid=start+((end-start)/2);
// while(start<=end){
// if (vect[mid]<target){
// return binarySearch(vect, target,mid+1,end);
// }
// else if (vect[mid]>target){
// return binarySearch(vect,target,start,mid-1);
// }
// else{
// return mid;
// }
// }
// return -1;
// }
// int main(){
// vector<int> vect={3,4,5,7,8,9,10};
// int n=vect.size();
// int target;
// cout<<"Enter target: ";
// cin>>target;
// int answer=binarySearch(vect,target,0,n-1);
// cout<<"Target found at index: "<<answer;
// }