forked from shikaruki/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search.cpp
46 lines (42 loc) · 841 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
#include <iostream>
using namespace std;
int lin(int arr[],int n,int key){
int low=0;
int high=n-1;
int num;
while(low<=high)
{
num=(low+high)/2;
if(key==arr[num])
return num;
else if(key<arr[num]){
high=num-1;
}
// if(key>num){
else{
low=num+1;
}
}
return -1;
}
int main()
{
int key,n,i;
int arr[n];
cout<<"enter range ";
cin>>n;
for(i=0;i<n;i++){
cout<<"eneter the "<<i+1<<" number";
cin>>arr[i];
}
//int num
cout<<"enter the key ";
//cin>>key;
int data=lin(arr,n,5);
if(data==-1)
cout<<"the number you entered is out of scope";
else
cout<<"the number is placed at "<<data <<"position";
//cout<<"here the num you wants "<<data;
return 0;
}