forked from Annex5061/java-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary.java
41 lines (36 loc) · 1.11 KB
/
Binary.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
38
39
40
41
import java.util.Scanner;
import java.time.Duration;
import java.time.Instant;
public class Binary {
public static int binary_search(int[]A,int target){
int lo=0;
int hi=A.length-1;
while(lo<=hi)
{
int mid=lo+(hi-lo)/2;
if(A[mid]==target)
return mid;
else if(A[mid]<target)
lo=mid+1;
else
hi=mid-1;
}
return -1;
}
public static void main(String[] args) {
long start=System.currentTimeMillis();
Scanner sc=new Scanner(System.in);
System.out.print("Enter How Many Elements:");
int n=sc.nextInt();
int[]A=new int[n];
System.out.println("Enter Digits:");
for(int i=0;i<A.length;i++)
A[i]=sc.nextInt();
System.out.println("Enter Target:");
int target=sc.nextInt();
System.out.println("element found at position: " +(binary_search(A, target)));
long end=System.currentTimeMillis();
System.out.println(" ");
System.out.println("Total Time take:"+(end-start));
}
}