-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary-Search.cpp
49 lines (43 loc) · 923 Bytes
/
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int binarySearch(vector <int> arr, int n, int key){
int s = 0;
int e = n;
while(s<=n){
int mid = (s+e)/2;
if(arr[mid]==key){
return mid;
}
else if(arr[mid]>key){
e=mid-1;
}
else{
s=mid+1;
}
}
return -1;
}
int main()
{
vector <int> arr;
int n; // lenght of array
int ele; // for adding element to the array
cin>>n;
for(int i = 0; i<n; i++){
cin>>ele;
arr.push_back(ele);
}
int key;
cout<<"Enter the key to find in the array: "<<endl;
cin>>key;
sort(arr.begin(), arr.end());
cout<<"The sorted array : "<<endl;
for(int i = 0; i<n; i++){
cout<<arr.at(i)<<" ";
}
cout<<endl;
cout<<binarySearch(arr, n, key)<<endl;
return 0;
}