forked from Annex5061/java-algorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.java
More file actions
Latest commit
41 lines (36 loc) · 1.11 KB
/
Copy pathBinary.java
File metadata and controls
41 lines (36 loc) · 1.11 KB
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
importjava.util.Scanner;
importjava.time.Duration;
importjava.time.Instant;
publicclassBinary {
publicstaticintbinary_search(int[]A,inttarget){
intlo=0;
inthi=A.length-1;
while(lo<=hi)
{
intmid=lo+(hi-lo)/2;
if(A[mid]==target)
returnmid;
elseif(A[mid]<target)
lo=mid+1;
else
hi=mid-1;
}
return -1;
}
publicstaticvoidmain(String[] args) {
longstart=System.currentTimeMillis();
Scannersc=newScanner(System.in);
System.out.print("Enter How Many Elements:");
intn=sc.nextInt();
int[]A=newint[n];
System.out.println("Enter Digits:");
for(inti=0;i<A.length;i++)
A[i]=sc.nextInt();
System.out.println("Enter Target:");
inttarget=sc.nextInt();
System.out.println("element found at position: " +(binary_search(A, target)));
longend=System.currentTimeMillis();
System.out.println(" ");
System.out.println("Total Time take:"+(end-start));
}
}