forked from ccgcv/Cplus-plus-for-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.1
44 lines (38 loc) · 758 Bytes
/
binary.1
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
#include<bits/stdc++.h>
using namespace std;
int binarysearch(int arr[],int size,int key)
{
int start = 0,end = size -1;
int mid = start + (end - start)/2;
while(start<=end)
{
if(arr[mid]==key)
{
return mid;
}
else if(arr[mid]>key)
{
end = mid -1;
}
else
start = mid+1;
mid = start + (end - start)/2;
}
return -1;
}
int main()
{
int arr[10000],n,key;
cout<<"enter the size of the array: ";
cin>>n;
cout<<"enter the key : ";
cin>>key;
int index;
for(index=0;index<n;index=index+1)
{
cout<<"arr["<<index<<"] : ";
cin>>arr[index];
}
cout<<binarysearch(arr,n,key);
return 0;
}