-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySear.java
37 lines (36 loc) · 881 Bytes
/
BinarySear.java
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
public class BinarySear {
int binearsearch(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 binearsearch(arr, l, mid-1, x);
}
if (arr[mid]<x)
{
return binearsearch(arr, mid+1, r, x);
}
}
return -1;
}
public static void main(String[] args) {
int arr[]={1,23,44,56,78,93,99};
int n=arr.length;
int x=56;
BinarySear ob=new BinarySear();
int result=ob.binearsearch(arr, 0,n-1, x);
if(result==-1)
{
System.out.println("Elemnt is not founde");
}
else{
System.out.println("Element is found at index: "+result);
}
}
}