forked from shuvodas0/Searching-technique
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary
More file actions
36 lines (33 loc) · 726 Bytes
/
Binary
File metadata and controls
36 lines (33 loc) · 726 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int Binary_Search(int arr[],int n,int index)
{
int f=0, l=n;
while(f<l){
int mid=(f+l)/2;
if(arr[mid]==index){
return mid+1;
}
else if(arr[mid]>index){
l=mid-1;
}
else{
f=mid+1;
}
}
return -1;
}
int main(){
int n, arr[n], index;
cout<<"Enter the length of array: "<<endl;
cin>>n;
cout<<"Enter the array: "<<endl;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"Enter the element you want to search: "<<endl;
cin>>index;
cout<<"The element is at position: "<<Binary_Search(arr, n, index)<<endl;
return 0;
}