forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
33 lines (30 loc) · 804 Bytes
/
binarySearch.cpp
File metadata and controls
33 lines (30 loc) · 804 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
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int arr[], int low, int high, int x)
{
if (high >= low) {
int mid = (low + high) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, low, mid - 1, x);
return binarySearch(arr, mid + 1, high, x);
}
return -1;
}
int main()
{
int arr[100],n,x;
cout<<"Enter no of elements in the array"<<endl;
cin>>n;
cout<<"Enter the elements of the array :"<<endl;
for(int i = 0; i<n; i++)
{
cin>>arr[i];
}
cout<<"Enter the Number that you want to search : "<<endl;
cin>>x;
int res = binarySearch(arr, 0, n, x);
(res == -1) ? cout << "Number not present": cout << "Number present at index " << res;
return 0;
}