We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 13adf55 commit 39ebe8aCopy full SHA for 39ebe8a
algorithms/searching/binary_search.cpp
@@ -0,0 +1,33 @@
1
+#include<bits/stdc++.h>
2
+using namespace std;
3
+
4
+int binarySearch(int a[],int n, int item){
5
+ int first=0, last=n,mid;
6
7
+ while(first<=last){
8
+ mid=(first+last)/2;
9
+ if(item==a[mid]) return mid;
10
+ else if(item < a[mid]) last=mid-1;
11
+ else first=mid+1;
12
+ }
13
+ return -1;
14
+}
15
16
+int main()
17
+{
18
+ ios_base::sync_with_stdio(false);
19
+ cin.tie(NULL);
20
+ int n;
21
+ cin>>n;
22
+ int a[n];
23
+ for(int i=0;i<n;i++){
24
+ cin>>a[i];
25
26
+ sort(a,a+n);
27
+ int item;
28
+ cin>>item;
29
+ int index=binarySearch(a,n,item);
30
+ if(index>=0) cout<<"Found at index "<<index;
31
+ else cout<<"Not found";
32
+return 0;
33
0 commit comments