-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinary_search.cpp
More file actions
44 lines (41 loc) · 821 Bytes
/
binary_search.cpp
File metadata and controls
44 lines (41 loc) · 821 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
#include <iostream>
using namespace std;
void binary_search(int n, int* arr, int s ){
int left = 0;
int right = n-1;
int m = (left+right)/2;
int c = 0;
if((arr[0]>s)||(arr[n-1]<s)){cout << "not there" << endl; return;}
while((m!=left)||(m!=right)){
if(arr[m] < s){
left = m+1;
m = (left+right)/2;
}
else if(arr[m] > s){
right = m-1;
m = (left+right)/2;
}
else{
c=1;
break;
}
}
if(c==1){cout << "found" << endl;return;}
else{cout << "not there" << endl;return;}
}
int main(){
int n;
cout << "Enter the number of elements of the sorted array " << endl;
cin >> n;
int arr[n];
for(int i=0;i<n;i++)
{
cout << "enter it" << endl;
cin >> arr[i];
}
int s;
cout << "Enter the number to be searched " << endl;
cin >> s;
binary_search(n,arr,s);
return 0;
}