-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
47 lines (43 loc) · 761 Bytes
/
Copy pathbinary_search.cpp
File metadata and controls
47 lines (43 loc) · 761 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
37
38
39
40
41
42
43
44
45
46
47
#include<iostream>
#include<cmath>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;
int binary_search(int array[],int size, int key)
{
int start = 0;
int end=size;
while (start<=end)
{
int mid=(start+end)/2;
if (array[mid]==key)
{
return mid+1;
}
else if (array[mid]>key)
{
end= mid-1;
}
else
{
start=mid+1;
}
}
return -1;
}
int main()
{
int n;
int key_main;
cout<<"Enter the size of the array"<<endl;
cin>>n;
int array[n];
for (int i = 0; i < n; i++)
{
cin>>array[i];
}
cout<<"Enter the number you want position of"<<endl;
cin>>key_main;
cout<<"Your no is at "<<binary_search(array,n,key_main)<<" position";
return 0;
}