-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Search.cpp
More file actions
36 lines (36 loc) · 851 Bytes
/
Binary Search.cpp
File metadata and controls
36 lines (36 loc) · 851 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
// THIS PROGRAM IS WRITTEN BY BALAJI G
#include <iostream>
using namespace std;
int binsearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binsearch(arr, l, mid - 1, x);
return binsearch(arr, mid + 1, r, x);
}
return -1;
}
int main(void)
{
int n;
cout<<"ENTER THE SIZE OF THE ARRAY:: \n";
cin>>n;
int a[n];
cout<<"ENTER THE ELEMENTS::\n";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int k;
cout<<"ENTER THE ELEMENT YOU WANT TO SEARCH:: \n";
cin>>k;
int least=0;
int p=binsearch(a,least,n-1,k);
if(p!=-1)
cout<<"ELEMENT FOUND AT INDEX: "<<p<<endl;
else cout<<"ELEMENT NOT FOUND!!";
return 0;
}