-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarysearch.cpp
38 lines (37 loc) · 910 Bytes
/
binarysearch.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
#include <iostream>
using namespace std;
void print(int arr[], int s, int e){
for (int i=s;i<=e;i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
bool binary_search(int *list,int start,int end,int key){
cout<<endl;
print(list,start,end);
if (start>end){
return false;}
int mid=(start+end)/2;
if (list[mid]==key){
return true;}
if (key>list[end] || list[start]>key){
return false;}
if (key<list[mid]){
return binary_search(list,start,mid-1,key);}
else{
return binary_search(list,mid+1,end,key);}
}
int main(){
int arr[]={1,2,3,7,9,99};
int s=0;
int e=5;
int key=999;
int ans=binary_search(arr,s,e,key);
if (ans){
cout<<"Element is present in list"<<endl;
}
else{
cout<<"Element is not present in list"<<endl;
}
return 0;
}