-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary2.java
More file actions
34 lines (32 loc) · 729 Bytes
/
Binary2.java
File metadata and controls
34 lines (32 loc) · 729 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
import java.util.Scanner;
public class Binary2
{
static int bsearch(int arr[] ,int low,int high,int x)
{
if(low>high)
return -1;
int mid=(low+high)/2;
if(arr[mid]==x)
return mid;
else if(arr[mid]>x)
return bsearch(arr,low,mid-1,x);
else
return bsearch(arr,mid+1,high,x);
}
public static void main(String[] args)
{
Binary2 obj=new Binary2();
Scanner t=new Scanner(System.in);
System.out.println("Enter the size of array");
int n=t.nextInt();
int[] arr=new int[n];
Scanner d=new Scanner(System.in);
System.out.println("Enter the elemments to be searched");
int x=d.nextInt();
for(int i=0;i<n;i++)
{
arr[i]=t.nextInt();
}
System.out.println(obj.bsearch(arr,0,n-1,x));
}
}